The Concurrent Mark Sweep collector

2015/11/20 14:24
阅读数 375

The Concurrent Mark Sweep collector

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.

Note: CMS collector on young generation uses the same algorithm as that of the parallel collector.


CMS Collection Phases

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.

(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.
(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.
(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.

注意:initial mark是由一个GC thread来执行,它的暂停时间相对比较短。而remark过程的暂停时间要比initial mark更长,且通常由多个thread执行。

  1. 初始标记 :在这个阶段,需要虚拟机停顿正在执行的任务,官方的叫法STW(Stop The Word)。这个过程从垃圾回收的"根对象"开始,只扫描到能够和"根对象"直接关联的对象,并作标记。所以这个过程虽然暂停了整个JVM,但是很快就完成了。

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

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

  4. 重新标记 :这个阶段会暂停虚拟机,收集器线程扫描在CMS堆中剩余的对象。扫描从"跟对象"开始向下追溯,并处理对象关联。

  5. 并发清理 :清理垃圾对象,这个阶段收集器线程和应用程序线程并发执行。

  6. 并发重置 :这个阶段,重置CMS收集器的数据结构,等待下一次垃圾回收。


Next, let's review CMS Collector operations step by step.

CMS Collector operations step by step

1.Heap Structure for CMS Collector

The heap is split into three spaces.

Young generation is split into Eden and two survivor spaces. Old generation is one contiguous(连续的) space. Object collection is done in place. No compaction is done unless there is a full GC.


How Young GC works in CMS

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.

With CMS, old generation objects are deallocated(释放) in place. They are not moved around. The space is not compacted(压缩) unless there is a full GC.


Young Generation Collection

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.


After Young GC

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.


Old Generation Collection with CMS

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.


Old Generation Collection - Concurrent Sweep

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

Note: Unmarked objects == Dead Objects


Old Generation Collection - After Sweeping

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.


1 收藏
0 评论
1 收藏