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:

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