Understanding G1 Garbage Collector: Architecture, Algorithms, and Tuning
This article explains the different types of Java garbage collectors, focuses on the G1 (Garbage‑First) collector’s region‑based architecture, key data structures and algorithms such as TLAB, PLAB, CSet, Card Table, RSet, and SATB, and provides detailed log analysis and tuning recommendations to improve pause times and throughput.
This article summarizes the various garbage collector (GC) types in the JVM, then concentrates on the G1 (Garbage‑First) collector, describing its region‑based design, important algorithms, and data structures, and finally offers log‑analysis techniques and tuning advice.
GC Classification
The main heap areas are Young Generation, Old Generation, and Permanent Generation (replaced by Metaspace after JDK 8). Modern collectors use generational collection to improve efficiency. Full GC scans the entire heap, while G1 aims to avoid full GC by adjusting region sizes.
Serial Garbage Collector
Single‑threaded, stops all application threads (Stop‑The‑World). Suitable for client‑mode or single‑CPU environments. Uses Serial for young generation (copying) and SerialOld for old generation (mark‑compact). Enabled with -XX:+UseSerialGC.
Serial: copying algorithm for young generation.
SerialOld: mark‑compact algorithm for old generation.
Parallel Garbage Collector
Multi‑threaded version of the serial collector, still Stop‑The‑World. Works well on server‑mode and multi‑CPU systems. Includes ParNew (parallel young), Parallel Scavenge (throughput‑focused young), and Parallel Old (parallel old).
ParNew: parallel young collector, threads equal to CPU count, enabled with -XX:+UseParNewGC.
Parallel Scavenge: focuses on throughput, configurable via -XX:MaxGCPauseMillis and -XX:GCTimeRatio, enabled with -XX:+UseParallelGC.
Parallel Old: parallel old collector, enabled with -XX:+UseParallelOldGC.
Concurrent Mark‑Sweep (CMS)
Uses a mark‑sweep algorithm for the old generation, runs concurrently with application threads to reduce pause time, but can cause fragmentation. The phases are Initial Mark (STW), Concurrent Mark, Final Remark (STW), and Concurrent Sweep.
Enable with -XX:+UseConcMarkSweepGC (young uses ParNew, old uses CMS, SerialOld acts as fallback).
G1 Garbage Collector
Introduced in JDK 7u4 and default since JDK 9. Activated with -XX:+UseG1GC. G1 divides the heap into many equal‑sized regions (1 MiB–32 MiB). It performs Young GC, Mixed GC (partial old‑generation collection), and Full GC (fallback to Serial Old).
G1 Region Concept
Regions are of five types: Eden, Survivor, Old, Humongous (objects > 50 % of a region), and Free. Regions are not fixed to a generation; after a Young GC, an Eden region may become Free and later be reused for other purposes.
Important G1 Data Structures and Algorithms
TLAB (Thread‑Local Allocation Buffer) – per‑thread buffer in Eden for fast allocation without locks. Sample OpenJDK code:
HeapWord* G1CollectedHeap::allocate_new_tlab(size_t min_size,
size_t requested_size,
size_t* actual_size) {
assert_heap_not_locked_and_not_at_safepoint();
assert(!is_humongous(requested_size), "we do not allow humongous TLABs");
return attempt_allocation(min_size, requested_size, actual_size);
}
inline HeapWord* G1CollectedHeap::attempt_allocation(size_t min_word_size,
size_t desired_word_size,
size_t* actual_word_size) {
assert_heap_not_locked_and_not_at_safepoint();
// exclude humongous objects
assert(!is_humongous(desired_word_size), "attempt_allocation() should not "
"be called for humongous allocation requests");
// try to allocate in current region
HeapWord* result = _allocator->attempt_allocation(min_word_size, desired_word_size, actual_word_size);
// if not enough space, request a new region
if (result == NULL) {
*actual_word_size = desired_word_size;
result = attempt_allocation_slow(desired_word_size);
}
assert_heap_not_locked();
if (result != NULL) {
assert(*actual_word_size != 0, "Actual size must have been set here");
dirty_young_block(result, *actual_word_size);
} else {
*actual_word_size = 0;
}
return result;
}PLAB (Promotion Local Allocation Buffer) – buffer used by GC threads during Young GC to promote objects from Eden/Survivor to Old.
Collection Set (CSet) – the set of regions selected for collection in a GC cycle.
Card Table – byte array that tracks dirty cards (small sub‑regions) for efficient remembered‑set updates.
Remembered Set (RSet) – per‑region structure that records references from other regions; used during concurrent marking to avoid scanning the entire old generation.
SATB (Snapshot‑At‑The‑Beginning) – incremental marking algorithm used by G1 during the concurrent marking phase; it records a snapshot of live objects at the start of marking.
G1 GC Processes
Young GC – triggered when Eden regions are full; copies live objects to Survivor or Old regions, stops the world for root scanning.
Mixed GC – follows a Young GC; collects a subset of old regions together with the young ones, reusing the root scan from the preceding Young GC.
Full GC – occurs when G1 cannot allocate space (e.g., huge object allocation failure); falls back to Serial Old and incurs a long stop‑the‑world pause.
G1 GC Log Analysis
Typical log lines include pause type, parallel time, GC worker statistics, and memory statistics. Example snippets:
2018-05-26T19:51:45.808-0800: 127.031: [GC pause (G1 Evacuation Pause) (young), 0.0063650 secs]
[Parallel Time: 5.5 ms, GC Workers: 4]
...
[Eden: 39.0M(39.0M)->0.0B(2048.0K) Survivors: 3072.0K->4096.0K Heap: 111.4M(128.0M)->72.9M(128.0M)]Key sections to watch: Ext Root Scanning, Update RS, Scan RS, Object Copy, Termination, and Other (Choose CSet, Ref Proc, Redirty Cards, Humongous Register/Reclaim, Free CSet).
Important G1 Parameters
-XX:+UseG1GC– enable G1. -XX:MaxGCPauseMillis – target maximum pause time. -XX:G1HeapRegionSize – size of each region (1 MiB–32 MiB). -XX:MaxTenuringThreshold – age threshold for promotion. -XX:InitiatingHeapOccupancyPercent – heap occupancy that starts concurrent marking. -XX:G1NewSizePercent / -XX:G1MaxNewSizePercent – young generation size bounds. -XX:ConcGCThreads – number of concurrent GC threads. -XX:G1HeapWastePercent – allowed heap waste before Mixed GC. -XX:G1MixedGCCountTarget – max Mixed GC cycles after a concurrent mark.
G1 Tuning Recommendations
Adjust -XX:MaxGCPauseMillis together with young‑generation size parameters to approach the desired pause time. Monitor -XX:+PrintAdaptiveSizePolicy for ergonomics information. Tune concurrent‑marking thresholds ( -XX:InitiatingHeapOccupancyPercent, -XX:ConcGCThreads) to balance pause time and throughput. If reference processing dominates pause time, consider -XX:+PrintReferenceGC and adjust -XX:SoftRefLRUPolicyMSPerMB for soft references.
Conclusion
The article provides an overview of G1 GC architecture, its key data structures, the various GC phases, log interpretation, and a set of practical tuning parameters. While default settings work for many workloads, careful monitoring and parameter adjustment are essential for latency‑sensitive or large‑heap applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Big Data Technology & Architecture
Wang Zhiwu, a big data expert, dedicated to sharing big data technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
