The G1 collector can perform some expensive GC operations concurrently to the application. It's designed to scale from small to large multi-core machines with a large amount (tens of GBs, with more than 50% of the heap live) of memory. It tolerates a significant amount of heap fragmentation, and has predictable pause times not longer than a few hundred milliseconds.
The collector focuses on the young generation, with occasional space-reclamation in the old generation. Whole-heap operations are performed concurrently with the application, and short stop-the-world pauses are performed to incrementally reclaim space through evacuation and compaction of live objects to new memory areas.
The heap is broken into a set of equally-sized heap regions, the units of memory allocation and reclamation, each a contiguous range of virtual memory. Old generation regions may be "humongous" where objects span multiple regions: such objects are always allocated directly in the old generation and are never moved by the collector.
Operation is in two phases:
Young-only GCs gradually fill the old generation with new objects.
- A few normal young-only collections promote objects to the old generation.
- When the old generation occupancy reaches the Initiating Heap Occupancy threshold, a concurrent start young collection is scheduled instead of a normal young collection:
- Concurrent start: begins marking all reachable (live) objects in the old regions to be retained. Normal young collections may occur in the meantime.
- Remark: finalises marking, processes global references and class unloading, reclaims empty regions and cleans up internal data structures.
- Between remark and cleanup: determines which free space may be reclaimed concurrently in old generations.
- Cleanup: pauses to determine whether a space reclamation will follow. If so, a mixed collection is performed.
Space reclamation attempts to reclaim space in multiple mixed collections, evacuating live objects in the old generation regions. This phase ends when the collector determines that evacuating old generation regions wouldn't yield enough free heap space to justify the effort. A full heap stop-the-world compaction is performed if the application runs out of memory gathering liveness information.
The goal run-time for gaps between GC pauses is set with
-XX:GCPauseTimeInterval. The collector attempts to meet this goal based on long-term observed pause time and approximations based on young generations of similar sizes.
The number of threads is set with
The number of threads used for concurrent work is set with
-XX:ConcGCThreads, and defaults to
The young generation is dynamically sized between
-XX:MaxNewSize; setting only one of these options fixes the young generation size to that value) to reduce GC pauses.
-XX:G1MixedGCCountTargetsets the target number of mixed GCs after a marking cycle to collect old regions with at most
-XX:G1MixedGCLiveThresholdPercentlive data (defaults to 8).
-XX:G1HeapWastePercentsets the maximum amount of heap space allowed to be wasted. Whilst below this threshold, the space reclamation phase will not execute.
-XX:G1PeriodicGCIntervalmay trigger consideration for GC at the specified minimum interval (milliseconds), allowing recovery of heap space while the application is idle.
-XX:+G1PeriodicGCInvokesConcurrentdetermines whether periodic GC should trigger concurrent (default; +) or full (-) GC.
-XX:G1PeriodicGCSystemLoadThresholdallows disabling periodic GC if the 1 minute system load average is above the specified threshold.
-XX:+G1UseAdaptiveIHOPtoggles the default calculated optimal value (which is based on the spent marking and how much memory is typically allocated in the old generations during marking cycles). If disabled,
-XX:InitialHeapOccupancyPercentis used as a constant instead of an initial value.
-XX:InitiatingHeapOccupancyPercentsets the percentage of the old generation occupancy at whichb to start the first few concurrent marking cycles (defaults to 45%).
-XX:G1AdaptiveIHOPNumInitialSamplessets the number of completed marking cycles which must be completed before sampling for adaptive calculation of
-XX:InitialHeapOccupancyPercent(defaults to 3).
-XX:G1HeapReservePercentsets the buffer on the IHOP value before old generation occupancy triggers space reclamation.
-XX:G1HeapRegionSizesets the size of each heap region, defaults to approximately
-XX:MaxHeapSize / 2and must be a power of 2 between 1MB to 32MB.
-XX:+G1EagerReclaimHumongousObjectstoggles attempts to reclaim humongous objects if they're not referenced by many objects during a GC pause (defaults to +).
-XX:-UseStringDeduplicationpoints identical string objects at the same character array (defaults to -).
-XX:StringDeduplicationAgeThresholdsets the age threshold for string deduplication (defaults to 3).
-XX:-ExplicitGCInvokesConcurrenttoggles whether explicit GCs should trigger concurrent (defaults to -).
-XX:-ParallelRefProcEnabledtoggles parallel processing of reference objects.