The Concurrent Mark Sweep collector

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

 

CMS Collection Phases

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

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

(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執行。this

  1. 初始標記 :在這個階段,須要虛擬機停頓正在執行的任務,官方的叫法STW(Stop The Word)。這個過程從垃圾回收的"根對象"開始,只掃描到可以和"根對象"直接關聯的對象,並做標記。因此這個過程雖然暫停了整個JVM,可是很快就完成了。spa

  2. 併發標記 :這個階段緊隨初始標記階段,在初始標記的基礎上繼續向下追溯標記。併發標記階段,應用程序的線程和併發標記的線程併發執行,因此用戶不會感覺到停頓。線程

  3. 併發預清理 :併發預清理階段仍然是併發的。在這個階段,虛擬機查找在執行併發標記階段新進入老年代的對象(可能會有一些對象重新生代晉升到老年代, 或者有一些對象被分配到老年代)。經過從新掃描,減小下一個階段"從新標記"的工做,由於下一個階段會Stop The World。orm

  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.

===========END===========

相關文章
相關標籤/搜索