天天看點

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

1.The G1 Garbage Collector(G1垃圾回收器定義)

The Garbage-First (G1) collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with a high probability, while achieving high throughput. The G1 garbage collector is fully supported in Oracle JDK 7 update 4 and later releases. The G1 collector is designed for applications that:

  • Can operate concurrently with applications threads like the CMS collector.
  • Compact free space without lengthy GC induced pause times.
  • Need more predictable GC pause durations.
  • Do not want to sacrifice a lot of throughput performance.
  • Do not require a much larger Java heap.

G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS). Comparing G1 with CMS, there are differences that make G1 a better solution. One difference is that G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions. This considerably simplifies parts of the collector, and mostly eliminates potential fragmentation issues. Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets.

釋義:G1使用并發和并行階段實作其目标暫停時間,并保持良好的吞吐量,與CMS收集器不同的是,G1回收算法采取的并發-整理 ;它可以解決傳統的parNew+CMS垃圾收集算法中,有很多晉代失敗(promotion failed)/并發失敗(concurrent mode failure),然後做Full gc的問題;G1提供更可預測的垃圾收集停頓比CMS收集器,并允許使用者指定所需的停頓的目标。

2.G1 heap(G1堆)

2.1 G1 Heap Structure 

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

Region size is chosen by the JVM at startup. The JVM generally targets around 2000 regions varying in size from 1 to 32Mb.

2.2 G1 Heap Allocation

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

The heap is partitioned into a set of equal-sized heap regions, each a contiguous range of virtual memory. Certain region sets are assigned the same roles (eden, survivor, old) as in the older collectors, but there is not a fixed size for them. This provides greater flexibility in memory usage.

2.2 G1 Footprint

If you migrate from the ParallelOldGC or CMS collector to G1, you will likely see a larger JVM process size. This is largely related to "accounting" data structures such as Remembered Sets and Collection Sets.

Remembered Sets or RSets track object references into a given region. There is one RSet per region in the heap. The RSet enables the parallel and independent collection of a region. The overall footprint impact of RSets is less than 5%.

Collection Sets or CSets the set of regions that will be collected in a GC. All live data in a CSet is evacuated (copied/moved) during a GC. Sets of regions can be Eden, survivor, and/or old generation. CSets have a less than 1% impact on the size of the JVM.

2.3 Recommended Use Cases for G1 (推薦使用G1場景)

The first focus of G1 is to provide a solution for users running applications that require large heaps with limited GC latency. This means heap sizes of around 6GB or larger, and stable and predictable pause time below 0.5 seconds.

 3.G1 Collection Phases

The G1 collector performs the following phases on the old generation of the heap. Note that some phases are part of a young generation collection.

Phase Description 釋義

(1) Initial Mark

(Stop the World Event)

This is a stop the world event. With G1, it is piggybacked on a normal young GC. Mark survivor regions (root regions) which may have references to objects in old generation. 初始标記:此階段,G1GC對根進行标記。該階段與年輕代垃圾(STW)回收密切相關
(2) Root Region Scanning Scan survivor regions for references into the old generation. This happens while the application continues to run. The phase must be completed before a young GC can occur. 根區域掃描:G1GC 在初始标記的存活區掃描對老年代的引用,并标記被引用的對象.該階段與應用程式同時運作,該階段完成後,才能開始下一次年輕代垃圾回收。
(3) Concurrent Marking Find live objects over the entire heap. This happens while the application is running. This phase can be interrupted by young generation garbage collections. 并發标記階段:G1GC 在整個堆中查找存活對象。該階段與應用程式同時運作,可以被年輕代垃圾回收中斷

(4) Remark

(Stop the World Event)

Completes the marking of live object in the heap. Uses an algorithm called snapshot-at-the-beginning (SATB) which is much faster than what was used in the CMS collector. 重新标記階段:該階段是 STW 回收,幫助完成标記周期。G1 GC 清空 SATB 緩沖區,跟蹤未被通路的存活對象,并執行引用處理。

(5) Cleanup

(Stop the World Event and Concurrent)

  • Performs accounting on live objects and completely free regions. (Stop the world)
  • Scrubs the Remembered Sets. (Stop the world)
  • Reset the empty regions and return them to the free list. (Concurrent)
清理階段:在這個最後階段,G1 GC 執行統計和 RSet 清理的STW操作。在統計期間,G1GC 會識别完全空閑的區域和可供進行混合垃圾回收的區域。

(*) Copying

(Stop the World Event)

These are the stop the world pauses to evacuate or copy live objects to new unused regions. This can be done with young generation regions which are logged as 

[GC pause (young)]

. Or both young and old generation regions which are logged as 

[GC Pause (mixed)]

.
複制:

回收過程按回收區域集合(CollectionSets/CSets)劃分.CSets可以包含(Eden,survivor/old)中的region,CSets占用記憶體一般小于整個jvm的1%;

年輕代垃圾回收:

G1 Young GC同時回收eden區域和上次垃圾回收的存活區域。Eden 和survivor的存活對象被疏散(複制/移動)到新的區域集。

被疏散對象的目标區域取決于對象的年齡,達到晉升年齡的對象疏散到老年代區域,

否則疏散到存活區,并将它包含在下一次年輕代或混合垃圾回收的CSet中。

混合垃圾回收:

G1 Mixd GC同時回收Eden,survivor,old的存活區域。當成功完成并發标記周期後,G1 GC從執行年輕代垃圾回收切換為執行混合垃圾回收。

在混合垃圾回收期間,G1GC 将一些舊的區域添加到 eden 和存活區供将來回收。G1GC 回收了足夠的舊區域後(經過多次混合垃圾回收),

G1将恢複執行年輕代垃圾回收,直到下一次标記周期完成。

4.G1 youngGC

 4.1 The heap is split into approximately 2000 regions. Minimum size is 1Mb and maximum size is 32Mb. Blue regions hold old generation objects and green regions hold young generation objects.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

Note that the regions are not required to be contiguous like the older garbage collectors.

4.2 Live objects are evacuated (i.e., copied or moved) to one or more survivor regions. If the aging threshold is met, some of the objects are promoted to old generation regions.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

This is a stop the world (STW) pause. Eden size and survivor size is calculated for the next young GC. Accounting information is kept to help calculate the size. Things like the pause time goal are taken into consideration.

This approach makes it very easy to resize regions, making them bigger or smaller as needed.

4.3 

End of a Young GC with G1

Live objects have been evacuated to survivor regions or to old generation regions.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

Recently promoted objects are shown in dark blue. Survivor regions in green.

In summary, the following can be said about the young generation in G1:

  • The heap is a single memory space split into regions.
  • Young generation memory is composed of a set of non-contiguous regions. This makes it easy to resize when needed.
  • Young generation garbage collections, or young GCs, are stop the world events. All application threads are stopped for the operation.
  • The young GC is done in parallel using multiple threads.
  • Live objects are copied to new survivor or old generation regions.

5.g1 oldGC

 5.1 Initial Marking Phase

    Initial marking of live object is piggybacked on a young generation garbage collection. In the logs this is noted as 

GC pause (young)(inital-mark)

.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

5.2  Concurrent Marking Phase

If empty regions are found (as denoted by the "X"), they are removed immediately in the Remark phase. Also, "accounting" information that determines liveness is calculated.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

  5.3  Remark Phase

Empty regions are removed and reclaimed. Region liveness is now calculated for all regions.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

5.4  Copying/Cleanup Phase G1 selects the regions with the lowest "liveness", those regions which can be collected the fastest. Then those regions are collected at the same time as a young GC. This is denoted in the logs as 

[GC pause (mixed)]

. So both young and old generations are collected at the same time.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

5.5  After Copying/Cleanup Phase

The regions selected have been collected and compacted into the dark blue region and the dark green region shown in the diagram.

JAVA虛拟機-G1 Heap Structure(四)1.The G1 Garbage Collector(G1垃圾回收器定義)2.G1 heap(G1堆) 3.G1 Collection Phases4.G1 youngGC5.g1 oldGC

5.6 Summary of Old Generation GC

In summary, there are a few key points we can make about the G1 garbage collection on the old generation.

  • Concurrent Marking Phase
    • Liveness information is calculated concurrently while the application is running.
    • This liveness information identifies which regions will be best to reclaim during an evacuation pause.
    • There is no sweeping phase like in CMS.
  • Remark Phase
    • Uses the Snapshot-at-the-Beginning (SATB) algorithm which is much faster then what was used with CMS.
    • Completely empty regions are reclaimed.
  • Copying/Cleanup Phase
    • Young generation and old generation are reclaimed at the same time.
    • Old generation regions are selected based on their liveness.

6.G1相關參數

相關文章:

  • JAVA虛拟機-JMM記憶體模型(六)
  • JAVA虛拟機-JVM性能調優(五)
  • JAVA虛拟機-G1 Heap Structure(四)
  • JAVA虛拟機-CMS Heap Structure(三)
  • JAVA虛拟機-GC介紹和垃圾算法了解(二)
  • JAVA虛拟機-Java體系結構及hotspot介紹(一)