Garbage-First垃圾收集器如何工作?

dog*_*ane 43 java garbage-collection g1gc

有人可以解释G1垃圾收集器如何工作吗?我无法在任何地方找到任何全面,易于理解的描述.

谢谢

Eug*_*hov 43

收集器将堆分成固定大小的区域并跟踪这些区域中的实时数据.它保留了一组指针 - "记忆集" - 进出该地区.当认为有必要时,它首先收集具有较少实时数据的区域(因此,"垃圾优先").通常,这可能意味着一步收集整个区域:如果指向区域的指针数为零,则不需要对该区域进行标记或扫描.

对于每个区域,它会跟踪各种指标,这些指标描述了收集它们需要多长时间.您可以为其提供关于暂停时间的软实时约束,然后尝试在该约束时间内收集尽可能多的垃圾.

有JavaOne谈论G1,关于这个主题的文章很少:

  • 谢谢,drdobbs文章非常好. (3认同)

Ale*_*leš 32

G1在这个新的JavaOne 2012会话中得到了很好的解释:G1垃圾收集器性能调整 [ youtube ],[ PDF ].

他们首先介绍CMS和G1,他们的比较,然后解释G1分析和调整.

G1特点

  • 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:

典型的G1堆可能看起来像:

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