The Garbage-First Garbage Collector (or G1 GC for short) is a new GC that is being introduced in the Java HotSpot VM in JDK 7. An experimental version of G1 has also been released in Java SE 6 Update 14. G1 is the long-term replacement for HotSpot's low-latency Concurrent Mark-Sweep GC (widely referred to as CMS).
G1 is a “server-style” GC and has the following attributes.
G1 takes a very different approach to heap layout than other HotSpot GCs. In G1, there is no physical separation between the young and old generations. Instead, there is a single contiguous heap which is split into same-sized regions. The young generation is a set of potentially non-contiguous regions, and the same is true for the old generation. This allows G1 to flexibly move resources as needed from the old to the young generation, and vice versa.
Collection in G1 takes place through evacuation pauses, during which the survivors from a set of regions referred to as the collection set are evacuated to another set of regions (the to-space) so that the collection set regions can then be reclaimed. Evacuation pauses are done in parallel, with all available CPUs participating. Most evacuation pauses collect the available young regions, thus are the equivalent of young collections in other HotSpot GCs. Occasionally, select old regions may also be collected during these pauses because G1 piggybacks old generation collection activity on young collections.
Like CMS, G1 periodically performs a concurrent marking phase. The main role of this phase is to identify which old regions are mostly full of garbage objects, as these are the most efficient and desirable to collect. Unlike CMS, G1 does not perform a concurrent sweeping phase. Instead, the most profitable old regions identified by the concurrent marking phase are collected during subsequent evacuation pauses (the piggybacking mentioned above).
G1 is still considered experimental and can be enabled with the following two parameters:
-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC
To set a GC pause time goal, use the following parameter:
-XX:MaxGCPauseMillis =50
(for a pause time target of 50ms)
With G1, a time interval can be specified during which a GC pause should last no longer than the time given above:
-XX:GCPauseIntervalMillis =200
(for a pause interval target of 200ms)
Note that the above two options represent goals, not promises or guarantees. They might work well in some situations but not in others, and the GC might not always be able to obey them.
Alternatively, the size of the young generation can be specified explicitly to impact evacuation pause times:
-XX:+G1YoungGenSize=512m
(for a 512 megabyte young generation)
G1 also uses the equivalent of survivor spaces, which are, naturally, a set of (potentially non-contiguous) regions. Their size can be specified with the usual parameters (e.g., -XX:SurvivorRatio=6
).
Finally, to run G1 at its full potential, try setting these two parameters which are currently disabled by default because they may uncover a rare race condition:
-XX:+G1ParallelRSetUpdatingEnabled -XX:+G1ParallelRSetScanningEnabled
One more thing to note is that G1 is very verbose compared to other HotSpot GCs when -XX:+PrintGCDetails
is set. This is because it prints per-GC-thread timings and other information very helpful in profiling and trouble-shooting. If you want a more concise GC log, please switch to using -verbosegc
(though it is recommended that the more detailed GC log be obtained).
G1 development is now focused primarily on resolving any remaining reliability issues and improving performance, and also on removing the following limitations: