Mastering G1 GC: Why It Exists, How It Works, and Tuning Tips
This article explains the origins, design goals, and detailed mechanisms of Java's G1 garbage collector—including regions, CSet, RSet, SATB, the full collection cycle, common pitfalls, and practical tuning recommendations—to help developers achieve predictable pause times and high throughput.
Introduction
Many developers ask about G1 tuning; this article answers common questions about its purpose, suitable scenarios, trade‑offs, detailed process, log interpretation, comparison with CMS, and optimization strategies.
1. Basic Knowledge
Motivation Before G1, the classic collectors (Serial, Parallel, CMS) each optimized one of three goals—minimal memory/CPU, maximum throughput, or minimal pause time—but all suffered from scanning the entire old generation, fixed young/old generation layout, and other limitations.
Design Goals G1 targets multi‑core, large‑memory servers, aiming to meet a specified pause‑time target while maintaining high throughput.
Use Cases Suitable when you need parallel GC threads, want predictable pause times, and do not require the absolute highest throughput.
2. Important Concepts
Region G1 divides the heap into equal‑sized regions (1 MiB–32 MiB). Each region can belong to the young or old generation at any moment, allowing selective collection of the most garbage‑filled regions.
CSet (Collection Set) A set of regions chosen for reclamation in a GC cycle; it may contain Eden, survivor, or old‑generation regions and typically occupies less than 1 % of the heap.
RSet (Remembered Set) Tracks which other regions contain references into a given region, enabling the collector to avoid scanning the entire heap for roots.
SATB (Snapshot‑At‑The‑Beginning) A concurrent marking algorithm that takes a logical snapshot of the heap at the start of a marking cycle; write barriers record old reference values into SATB buffers to keep the snapshot consistent.
3. G1 Collection Process
The collector performs four main operations:
Young‑generation GC
Concurrent background collection
Mixed GC (young + selected old regions)
Full GC when necessary
Young GC triggers when Eden is exhausted, pauses the application briefly, and copies surviving objects to survivor regions or the old generation.
Concurrent Marking Cycle consists of several phases:
Initial Mark (STW, often combined with a young GC) sets TAMS variables and marks roots.
Root‑Region Scan (no pause) scans survivor regions for reachable objects.
Concurrent Mark runs in parallel threads, tracing live objects and recording reference updates via SATB.
Remark (STW) processes remaining SATB buffers and finalizes marking.
Cleanup identifies reclaimable old regions, clears empty RSets, and performs minor compaction.
Mixed GC repeatedly reclaims a subset of old regions with the most garbage until most of the heap is reclaimed, then returns to regular young GC cycles.
Humongous Objects (size > ½ region) are allocated directly in old‑generation regions; they are never moved and can cause fragmentation. Adjust -XX:G1HeapRegionSize if many humongous objects waste space.
4. Tuning Guidelines
Do not set the young‑generation size manually ( -Xmn or -XX:NewRatio); let G1 adapt to the pause‑time target.
Set -XX:MaxGCPauseMillis=N to express the desired maximum pause.
If pauses remain too long, consider increasing concurrent threads ( -XX:ConcGCThreads) or triggering the marking cycle earlier ( -XX:InitiatingHeapOccupancyPercent).
Adjust mixed‑GC aggressiveness with -XX:G1MixedGCLiveThresholdPercent and -XX:G1MixedGCCountTarget.
5. Best Practices
Enable G1: -XX:+UseG1GC Target pause: -XX:MaxGCPauseMillis=200 Marking trigger:
-XX:InitiatingHeapOccupancyPercent=456. Common Questions
Difference between Young, Mixed, and Full GC – Young GC only collects young regions; Mixed GC also includes selected old regions; Full GC pauses all threads and compacts the entire heap.
ParallelGCThreads vs ConcGCThreads – The former controls threads used during STW phases; the latter controls threads used during concurrent marking.
Write Barrier purpose – Prevents the mutator from creating black‑to‑white references during concurrent marking, ensuring the SATB snapshot remains valid.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
