`

JAVA虚拟机-CMS Heap Structure(三)

 
阅读更多

  1.Reviewing Generational GC and CMS

The Concurrent Mark Sweep (CMS) collector (also referred to as the concurrent low pause collector) collects the tenured generation. It attempts to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads. Normally the concurrent low pause collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap.

大致意思:CMS垃圾回收器是回收堆中老年代对象。它与应用线程并行做垃圾回收以减少最小停顿时间,它的最小停顿时间不是因为 复制或整理存活对象,最主要问题是因为碎片化,所以需要分配一个较大的堆。

Heap Structure for CMS Collector:(cms垃圾回收器堆结构)

 

2.CMS Collection Phases (CMS收集阶段)

 CMS以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.

The CMS collector performs the following phases on the old generation of the heap:

Phase Description
(1) Initial Mark
(Stop the World Event)
Objects in old generation are “marked” as reachable including those objects which may be reachable from young generation. Pause times are typically short in duration relative to minor collection pause times. 初始化标记:这个阶段从垃圾回收的"根对象"开始,只扫描到能够和"根对象"直接关联的对象,并作标记,在做youngGC会消耗很短的停顿时间,1-10ms级别
(2) Concurrent Marking Traverse the tenured generation object graph for reachable objects concurrently while Java application threads are executing. Starts scanning from marked objects and transitively marks all objects reachable from the roots. The mutators are executing during the concurrent phases 2, 3, and 5 and any objects allocated in the CMS generation during these phases (including promoted objects) are immediately marked as live.

并发标记:这个阶段紧随初始标记阶段,在初始标记的基础上继续向下追溯标记,应用程序的线程和并发标记的线程并发执行,用户不会感受到停顿。

并发预清理:并发预清理阶段仍然是并发的。在这个阶段,虚拟机查找在执行并发标记阶段新进入老年代的对象(可能会有一些对象从新生代晋升到老年代, 或者有一些对象被分配到老年代)。通过重新扫描,减少下一个阶段"重新标记"的工作,因为下一个阶段会Stop The World。

(3) Remark
(Stop the World Event)
Finds objects that were missed by the concurrent mark phase due to updates by Java application threads to objects after the concurrent collector had finished tracing that object. 重新标记:这个阶段会暂停虚拟机,收集器线程扫描在CMS堆中剩余的对象。扫描从"跟对象"开始向下追溯,并处理对象关联。
(4) Concurrent Sweep Collects the objects identified as unreachable during marking phases. The collection of a dead object adds the space for the object to a free list for later allocation. Coalescing of dead objects may occur at this point. Note that live objects are not moved. 并发清理:清理垃圾对象,这个阶段收集器线程和应用程序线程并发执行。
(5) Resetting Prepare for next concurrent collection by clearing data structures. 并发重置:这个阶段,重置CMS收集器的数据结构,等待下一次垃圾回收.

 

3.How Young GC works in CMS(CMS年轻代回收过程)

 3.1 The young generation is colored light green and the old generation in blue. This is what the CMS might look like if your application has been running for a while. Objects are scattered around the old generation area.

 

 3.2 Live objects are copied from the Eden space and survivor space to the other survivor space. Any older objects that have reached their aging threshold are promoted to old generation.

 

3.3 After a young GC, the Eden space is cleared and one of the survivor spaces is cleared.

 

Newly promoted objects are shown in dark blue on the diagram. The green objects are surviving young generation objects that have not yet been promoted to old generation. 

 

 

4.Old Generation Collection with CMS(CMS老年代垃圾回收)

 4.1 Two stop the world events take place: initial mark and remark. When the old generation reaches a certain occupancy rate, the CMS is kicked off.

 

(1) Initial mark is a short pause phase where live (reachable) objects are marked. (2) Concurrent marking finds live objects while the application continues to execute. Finally, in the (3) remark phase, objects are found that were missed during (2) concurrent marking in the previous phase.

 

4.2 Objects that were not marked in the previous phase are deallocated in place. There is no compaction.

 

Note: Unmarked objects == Dead Objects

 

4.3 After the (4) Sweeping phase, you can see that a lot of memory has been freed up. You will also notice that no compaction has been done.

 

Finally, the CMS collector will move through the (5) resetting phase and wait for the next time the GC threshold is reached.

 

5.CMS 相关参数解释

参数 含义

-XX:+UseConcMarkSweepGC

激活CMS收集器。默认HotSpot JVM使用的是并行收集器

-XX:UseParNewGC

当使用-XX:+UseConcMarkSweepGC时,-XX:UseParNewGC会自动开启。因此,

如果年轻代的并行GC不想开启,可以通过设置-XX:-UseParNewGC来关掉

-XX:+CMSConcurrentMTEnabled

当该标志被启用时,并发的CMS阶段将以多线程执行

(因此,多个GC线程会与所有的应用程序线程并行工作)。该标志已经默认开启,

如果顺序执行更好,这取决于所使用的硬件,多线程执行可以通过-XX:-CMSConcurremntMTEnabled禁用。

-XX:ConcGCThreads

标志-XX:ConcGCThreads=<value>(早期JVM版本也叫-XX:ParallelCMSThreads)定义并发CMS过程运行时的线程数。比如value=4意味着CMS周期的所有阶段都以4个线程来执行。尽管更多的线程会加快并发CMS过程,但其也会带来额外的同步开销。因此,对于特定的应用程序,应该通过测试来判断增加CMS线程数是否真的能够带来性能的提升。如果还标志未设置,JVM会根据并行收集器中的-XX:ParallelGCThreads参数的值来计算出默认的并行CMS线程数。该公式是ConcGCThreads = (ParallelGCThreads + 3)/4。因此,对于CMS收集器, -XX:ParallelGCThreads标志不仅影响“stop-the-world”垃圾收集阶段,还影响并发阶段。

-XX:CMSInitiatingOccupancyFraction

当堆满之后,并行收集器便开始进行垃圾收集,例如,当没有足够的空间来容纳新分配或提升的对象,JVM会在一开始执行CMS周期前作一些线索查找。该线索由 -XX:CMSInitiatingOccupancyFraction=<value>来设置,该值代表老年代堆空间的使用率.

-XX:+UseCMSInitiatingOccupancyOnly

 

-XX:+CMSClassUnloadingEnabled

相对于并行收集器,CMS收集器默认不会对永久代进行垃圾回收。如果希望对永久代进行垃圾回收,可用设置标志-XX:+CMSClassUnloadingEnabled。

-XX:+UseCMSCompactAtFullCollection

 打开对年老代的压缩.可能会影响性能,但是可以消除碎片,由于并发收集器不对内存空间进行压缩,整理,所以运行一段时间以后会产生"碎片",使得运行效率降低.此值设置运行多少次GC以后对内存空间进行压缩,整理.

-XX:+ExplicitGCInvokesConcurrent and -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses

 

-XX:+DisableExplicitGC

该标志将告诉JVM完全忽略系统的GC调用(不管使用的收集器是什么类型)

 

 

参考oracle官网 CMS和G1 垃圾回收器

 

相关文章:

 

  • 大小: 74.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics