plumbr整理

  • RMI & GC
    • JMX
      • Architecture of the JMX Technology
        • Instrumentation
          • MBeans
        • JMX agent
          • directly controls resources and makes them available to remote management applications
          • MBean server
        • Remote management
          • JMX technology instrumentation can be accessed in many different ways
          • The MBean server relies on protocol adaptors and connectors to make a JMX agent accessible
      • JMX 3層架構(gòu)圖
    • JConsole
      • based-on JMX(rmi adapter), used to connect MBean Server to manage remote MBeans
    • 1.publishing or consuming services over RMI
    • 2.third party libraries or utilities can still open RMI endpoints
      • One such common culprit is for example JMX, which, if attached to remotely, will use RMI underneath to publish the data
    • the JVM periodically launches full GC to make sure that locally unused objects are also not taking up space on the other end
    • When you check the old generation consumption, there is often no pressure to the memory as there is plenty of free space in the old generation, but full GC is triggered, stopping the application threads.
    • This behavior of removing remote references via System.gc() is triggered by the sun.rmi.transport.ObjectTable class requesting garbage collection to be run periodically as specified in the sun.misc.GC.requestLatency() method.
    • java -Dsun.rmi.dgc.server.gcInterval=9223372036854775807L -Dsun.rmi.dgc.client.gcInterval=9223372036854775807L com.yourcompany.YourApplication(兩個(gè)參數(shù)默認(rèn)都是1h, 最佳實(shí)踐是兩個(gè)參數(shù)都設(shè)置上由JVM選擇)
    • This sets the period after which System.gc() is run to Long.MAX_VALUE; for all practical matters, this equals eternity.
    • -XX:+DisableExplicitGC禁用System.gc()不推薦
  • JVMTI tagging
    • SetTag
      • jvmtiError SetTag(jvmtiEnv* env, jobject object, jlong tag)
      • Set the tag associated with an object. The tag is a long value typically used to store a unique identifier or pointer to object information. The tag is visible with GetTag.
    • Get Tag
      • jvmtiError GetTag(jvmtiEnv* env, jobject object, jlong* tag_ptr)
      • Retrieve the tag associated with an object. The tag is a long value typically used to store a unique identifier or pointer to object information. The tag is set with SetTag. Objects for which no tags have been set return a tag value of zero.
    • Whenever the application is run alongside with a Java Agent (-javaagent), there is a chance that the agent can tag the objects in the heap using JVMTI tagging. Agents can use tagging for various reasons that are not in the scope of this handbook, but there is a GC-related performance issue that can start affecting the latency and throughput of your application if tagging is applied to a large subset of objects inside the heap.
    • The problem is hidden in the native code where JvmtiTagMap::do_weak_oops iterates over all the tags during each garbage collection event and performs a number of not-so-cheap operations for all of them. To make things worse, this operation is performed sequentially and is not parallelized.
    • To check whether or not a particular agent can be the reason for extended GC pauses, you would need to turn on the diagnostic option of –XX:+TraceJVMTIObjectTagging. Enabling the trace will allow you to get an estimate of how much native memory the tag map consumes and how much time the heap walks take.
    • If you are not the author of the agent yourself, fixing the problem is often out of your reach. Apart from contacting the vendor of a particular agent you cannot do much. In case you do end up in a situation like this, recommend that the vendor clean up the tags that are no longer needed.
    • 實(shí)例
    • Humongous Allocations
      • 使用G1垃圾收集器才有
    • Conclusion
      • Therefore, there is no real silver bullet approach to tuning the JVM to match the performance goals you have to fulfill. What we have tried to do here is walk you through some common (and not so common) examples to give you a general idea of how problems like these can be approached. Coupled with the tooling overview and with a solid understanding of how the GC works, you have all the chances of successfully tuning garbage collection to boost the performance of your application.
  • Weak, Soft and Phantom References
    • Another class of issues affecting GC is linked to the use of non-strong references in the application. While this may help to avoid an unwanted OutOfMemoryError in many cases, heavy usage of such references may significantly impact the way garbage collection affects the performance of your application.
    • Weak Reference
      • Whenever GC discovers that an object is weakly reachable, that is, the last remaining reference to the object is a weak reference, it is put onto the corresponding ReferenceQueue,
      • One may then poll this reference queue and perform the associated cleanup activities. A typical example for such cleanup would be the removal of the now missing key from the cache.
      • The trick here is that at this point you can still create new strong references to the object, so before it can be, at last, finalized and reclaimed, GC has to check again that it really is okay to do this. Thus, the weakly referenced objects are not reclaimed for an extra GC cycle.
      • Many caching solutions build the implementations using weak referencing
    • Soft Reference
      • you should bear in mind that soft references are collected much less eagerly than the weak ones. The exact point at which it happens is not specified and depends on the implementation of the JVM. Typically the collection of soft references happens only as a last ditch effort before running out of memory. What it implies is that you might find yourself in situations where you face either more frequent or longer full GC pauses than expected, since there are more objects resting in the old generation.
    • Phantom Reference
      • do manual memory management
      • In order to ensure that a reclaimable object remains so, the referent of a phantom reference may not be retrieved: The get method of a phantom reference always returns null.
      • Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.
      • That is right, we have to manually clear() up phantom references or risk facing a situation where the JVM starts dying with an OutOfMemoryError. The reason why the Phantom references are there in the first place is that this is the only way to find out when an object has actually become unreachable via the usual means. Unlike with soft or weak references, you cannot resurrect a phantom-reachable object.

  • –XX:-UseAdaptiveSizePolicy(R大說只有PS實(shí)現(xiàn)了?)
    • 在使用cms算法下眼俊,如果開啟參數(shù)UseAdaptiveSizePolicy颇蜡,則每次minor gc后會重新計(jì)算eden稠茂,from和to的大小椭坚,計(jì)算過程依據(jù)的是gc過程統(tǒng)計(jì)的一些數(shù)據(jù),計(jì)算后的eden+from+to不會超過Xmx凳寺,同時(shí)from和to一般是不相等(初始化的時(shí)候from和to是相等的), 會導(dǎo)致MBean使用時(shí)拋出異常Memory pool not found, 可以先設(shè)置 –XX:-UseAdaptiveSizePolicy來workaround
  • GC Ergonomics
    • Parallel Scavenge收集器有一個(gè)參數(shù)-XX:+UseAdaptiveSizePolicy捧颅。當(dāng)這個(gè)參數(shù)打開之后盒卸,就不需要手工指定新生代的大小、Eden與Survivor區(qū)的比例结执、晉升老年代對象年齡等細(xì)節(jié)參數(shù)了度陆,虛擬機(jī)會根據(jù)當(dāng)前系統(tǒng)的運(yùn)行情況收集性能監(jiān)控信息,動態(tài)調(diào)整這些參數(shù)以提供最合適的停頓時(shí)間或者最大的吞吐量献幔,這種調(diào)節(jié)方式稱為GC自適應(yīng)的調(diào)節(jié)策略(GC Ergonomics)
  • 使用weak reference可能會導(dǎo)致minor->full
    • -Xmx24m -XX:NewSize=16m -XX:MaxTenuringThreshold=1
    • starting from using the object as keys in a weak hash map and ending with allocation profiling.
    • The root cause, of course, lies with the weak references. Before we added them, the objects created by the application were dying just before being promoted to the old generation. But with the addition, they are now sticking around for an extra GC round so that the appropriate cleanup can be done on them. Like before, a simple solution would be to increase the size of the young generation by specifying -Xmx64m -XX:NewSize=32m: The objects are now once again reclaimed during minor garbage collection.
    • demo analysis
  • SoftReference
  • PhantomReference
  • Could my JVMs be Affected?
    • -XX:+PrintReferenceGC
      • 打開時(shí)可能看到日志
        • 2.173: [Full GC (Ergonomics) 2.234: [SoftReference, 0 refs, 0.0000151 secs]2.234: [WeakReference, 2648 refs, 0.0001714 secs]2.234: [FinalReference, 1 refs, 0.0000037 secs]2.234: [PhantomReference, 0 refs, 0 refs, 0.0000039 secs]2.234: [JNI Weak Reference, 0.0000027 secs]...
    • As always, this information should only be analyzed when you have identified that GC is having impact to either the throughput or latency of your application. In such case you may want to check these sections of the logs. Normally, the number of references cleared during each GC cycle is quite low, in many cases exactly zero. If this is not the case, however, and the application is spending a significant period of time clearing references, or just a lot of them are being cleared, then further investigation is required.
  • What is the Solution?(some generic solutions to bear in mind)
    • Weak references – if the problem is triggered by increased consumption of a specific memory pool, an increase in the corresponding pool (and possibly the total heap along with it) can help you out. As seen in the example section, increasing the total heap and young generation sizes alleviated the pain.
    • Phantom references – make sure you are actually clearing the references. It is easy to dismiss certain corner cases and have the clearing thread to not being able to keep up with the pace the queue is filled or to stop clearing the queue altogether, putting a lot of pressure to GC and creating a risk of ending up with an OutOfMemoryError.
    • Soft references – when soft references are identified as the source of the problem, the only real way to alleviate the pressure is to change the application’s intrinsic logic.

  • Premature Promotion
    • promote rate
      • Notice that you can extract this information only from minor GC pauses. Full GC pauses do not expose the promotion rate as the change in the old generation usage in GC logs also includes objects cleaned by the major GC.
    • Why Should I Care?
      • Similarly to the allocation rate, the main impact of the promotion rate is the change of frequency in GC pauses. But as opposed to the allocation rate that affects the frequency of minor GC events, the promotion rate affects the frequency of major GC events.
    • https://github.com/gvsmirnov/java-perv/blob/master/labs-8/src/main/java/ru/gvsmirnov/perv/labs/gc/PrematurePromotion.java
  • High Allocation Rate

  • 工具

    • JMX API
      • You can access this API either programmatically from your own application running inside the very same JVM, or using JMX clients.
      • Two of the most popular JMX clients are JConsole and JVisualVM (with a corresponding plugin installed).
      • All the JMX clients run as a separate application connecting to the target JVM. The target JVM can be either local to the client if running both in the same machine, or remote. For the remote connections from client, JVM has to explicitly allow remote JMX connections. This can be achieved by setting a specific system property to the port where you wish to enable the JMX RMI connection to arrive:java -Dcom.sun.management.jmxremote.port=5432 com.yourcompanyYourApp
      • After connecting your JMX client to the JVM of interest and navigating to the MBeans list, select MBeans under the node “java.lang/GarbageCollector”
      • In my experience this information is not enough to make any conclusions about the efficiency of the garbage collector.
      • This approach can rarely be used as we will see in the next sections, which give better ways of getting beneficial insight into garbage collection activities.
    • JVisualVM
      • JVisualVM adds extra information to the basic JMX client functionality via a separate plugin called “VisualGC”. It provides a real-time view into GC events and the occupancy of different memory regions inside JVM.
      • The most common use-case for the Visual GC plugin is the monitoring of the locally running application, when an application developer or a performance specialist wants an easy way to get visual information about the general behavior of the GC during a test run of the application.
      • When compared with pure JMX tools, the VisualGC add-on to JVisualVM does offer slightly better insight to the JVM, so when you have only these two tools in your toolkit, pick the VisualGC plug-in. If you can use any other solutions referred to in this chapter, read on. Alternative options can give you more information and better insight. There is however a particular use-case discussed in the “Profilers” section where JVisualVM is suitable for – namely allocation profiling, so by no means we are demoting the tool in general, just for the particular use case.
  • References

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坚芜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子斜姥,更是在濱河造成了極大的恐慌鸿竖,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铸敏,死亡現(xiàn)場離奇詭異缚忧,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)杈笔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門闪水,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蒙具,你說我怎么就攤上這事球榆⌒喾剩” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵持钉,是天一觀的道長衡招。 經(jīng)常有香客問我,道長每强,這世上最難降的妖魔是什么始腾? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮空执,結(jié)果婚禮上浪箭,老公的妹妹穿的比我還像新娘。我一直安慰自己辨绊,他們只是感情好奶栖,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著门坷,像睡著了一般驼抹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上拜鹤,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天框冀,我揣著相機(jī)與錄音,去河邊找鬼敏簿。 笑死明也,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的惯裕。 我是一名探鬼主播温数,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蜻势!你這毒婦竟也來了撑刺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤握玛,失蹤者是張志新(化名)和其女友劉穎够傍,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挠铲,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡冕屯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拂苹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片安聘。...
    茶點(diǎn)故事閱讀 40,561評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出浴韭,到底是詐尸還是另有隱情丘喻,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布念颈,位于F島的核電站泉粉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏舍肠。R本人自食惡果不足惜搀继,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一窘面、第九天 我趴在偏房一處隱蔽的房頂上張望翠语。 院中可真熱鬧,春花似錦财边、人聲如沸肌括。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谍夭。三九已至,卻和暖如春憨募,著一層夾襖步出監(jiān)牢的瞬間紧索,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工菜谣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留珠漂,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓尾膊,卻偏偏與公主長得像媳危,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子冈敛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容