How does the Garbage-First Garbage Collector work?

社会主义新天地 提交于 2019-11-28 15:43:35
Eugene Kuleshov

The collector splits the heap up into fixed-size regions and tracks the live data in those regions. It keeps a set of pointers — the "remembered set" — into and out of the region. When a GC is deemed necessary, it collects the regions with less live data first (hence, "garbage first"). Often, this can mean collecting an entire region in one step: if the number of pointers into a region is zero, then it doesn't need to do a mark or sweep of that region.

For each region, it tracks various metrics that describe how long it will take to collect them. You can give it a soft real-time constraint about pause times, and it then tries to collect as much garbage as it can in that constrained time.

There is JavaOne talk about G1 and few articles on the topic:

The G1 is nicely explained also in this new JavaOne 2012 session : G1 Garbage Collector Performance Tuning [youtube], [PDF].

They start with introduction of CMS and G1, their comparison, and then the G1 analysis and tuning is explained.

G1 characteristics

  • Fixed Size Regions - heap split into regions (1Mb - 32MB, ~2000, determined by the VM).
  • Eden, survivor and OldGen represented as a logical set of regions.
  • Live objects are evacuated from one region to another

A typical G1 heap may look like:

Here is a summary of each G1 phase:

1. Young Collection

1.1 Young Phase - Minor GC

  • Evacuation - Stop-The-World Parallel minor GC where live objects are evacuated from Young Generation either into Survivor regions (tenuring) or OldGen regions (promotion).
  • Accounting - size of eden/survivor space for the next young GC is determined based on stats from each region and based on the pause-time target set by the app. G1 estimates how much time it will need for the next YoungGC.
  • Resizing - G1 can now easily reduce/resize eden/survivor regions.

1.2 Young / Initial Mark

  • GC young initial-mark is an initial marking phase for the OldGen collection that is executed in parallel with the YoungGC collection. The initial mark is a parallel concurrent marking process.

2. Old Gen Collection

2.1 Initial Mark - see 1.2.

2.2 GC remark

  • one stop-the-world pause, concurrenlty marking live objects
  • accounting - for each region, during remark, G1 is tracking liviness of the region(how many objects are live in each region), and references into the region (Remembered Set) - this tells G1 how long is takes to do a collection on this region.
  • Empty Regions reclaimed

2.3. GC pause (mixed)

  • choose regions with low liviness and collect some of them. Hence, we are collecting the "garbage first".
  • the actual collection of these regions is performed at the same time as the next Young GC, so there is no separate pause for the collection of the OldGen. Therefore, the GC pause (mixed) is a mixed collection of YoungGen and a portion of old Gen.
  • At the end of GC pause (mixed) there can be some garbage left in old gen regions, this will be collected later depending of future liviness, pause time target and number of unused regions.

3. Full GC

Note that G1 is intended to avoid Full GC as much as possible. As of Java 7u40, FullGC pauses in G1 are not optimized and are implemented as a single threaded operation. When using G1, try to avoid Full GC - if you see any FullGC pauses, your GC setup probably requires some tuning.

Resources

I found Oracle's page on this to be extremely helpful at explaining the concepts in an accessible manner without being too lengthy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!