JVM Garbage Collection Deep Dive: Algorithms, Collectors, Tuning & Graal JIT
This comprehensive guide covers JVM garbage collection from fundamentals to advanced tuning, explaining reference counting vs reachability analysis, four GC algorithms, six collectors (Serial, Parallel, CMS, G1, ZGC, Shenandoah), G1 internals (Region, RSet, SATB, Mixed GC), ZGC's colored pointers and concurrent compaction, GC tuning methodology with parameters, and JDK 17 Graal JIT optimizations.
How to Determine Object Reclaimability: Reference Counting vs Reachability Analysis
Core Conclusion
JVM uses two classic algorithms to decide if an object is reclaimable:
Reference Counting
Reachability Analysis
Java adopts Reachability Analysis , a fundamental interview topic.
Reference Counting
Core Principle
Each object has a reference counter :
Increment when a reference is created
Decrement when a reference is lost
When counter = 0, the object can be reclaimed
Pros and Cons
✅ Pros : Simple implementation, high determination efficiency, no full heap traversal
❌ Fatal Flaw : Cannot solve cyclic references
Cyclic Reference Demonstration
public class ReferenceCountingDemo {
public Object instance = null;
public static void main(String[] args) {
ReferenceCountingDemo a = new ReferenceCountingDemo();
ReferenceCountingDemo b = new ReferenceCountingDemo();
// Mutual references
a.instance = b;
b.instance = a;
// Nullify external references
a = null;
b = null;
// Now a and b both have reference count 1, never reclaimed!
System.gc();
}
}Two objects mutually reference each other; external references are gone, but their counters stay at 1. The garbage collector can never collect them, causing memory leaks. Java therefore abandoned pure reference counting.
Note: Python now combines reference counting with mark-sweep to handle cycles.
Real-World Usage
Early Python versions
Objective-C
Microsoft COM
Reachability Analysis Algorithm
Core Principle
Starting from a set of objects called GC Roots , the algorithm searches downward:
The traversed paths are called Reference Chains
If an object has no reference chain connecting it to GC Roots , it is unreachable and can be reclaimed, even if it participates in cyclic references .
What Are GC Roots? (Interview Must-Know)
Objects referenced in VM stack (local variable table) : Local variables and parameters in methods
Objects referenced in native method stack (JNI) : Objects used during native method calls
Objects referenced by class static attributes in method area : Variables modified by static Objects referenced by constants in method area : Constants modified by final static Objects held by synchronized locks : Objects locked via
synchronizedEven if garbage objects A and B form a cycle, they are marked reclaimable because no path from GC Roots reaches them.
Visual Analogy
GC Roots = tree roots
All objects = branches and leaves
Leaves disconnected from roots = dead leaves, blown away (garbage collected)
Pros and Cons
✅ Pros : Perfectly solves cyclic reference problem
❌ Cons : Complex implementation, requires traversing entire reference chain, causing Stop-The-World (STW)
Interview Bonus: Two-Phase Marking
Reachability analysis uses two marking phases:
First Mark : Unreachable objects are marked once
Second Mark :
If object overrides finalize() and hasn't been called, it's placed in
F-QueueFinalizerthread executes finalize() If finalize() re-establishes a reference chain, the object is rescued
Otherwise, after second mark it is truly reclaimed
⚠️ Note : finalize() is not recommended ! Execution time is uncertain, may not run, and can only rescue once. Deprecated since Java 9.
Algorithm Comparison
Basic Principle : Reference Counting → Counter reaches zero → reclaim; Reachability Analysis → Unreachable from GC Roots → reclaim
Implementation Difficulty : Reference Counting → Simple; Reachability Analysis → Complex
Determination Efficiency : Reference Counting → High; Reachability Analysis → Low (traverses reference chains)
Cyclic Reference Problem : Reference Counting → ❌ Cannot solve, typical memory leak; Reachability Analysis → ✅ Perfectly solves, cyclic islands discarded together
STW Time : Reference Counting → Almost none; Reachability Analysis → Longer
Adopted by Java : Reference Counting → ❌ No; Reachability Analysis → ✅ Yes
Representative Languages : Reference Counting → Early Python, Objective-C; Reachability Analysis → Java, C#, Modern Python
Garbage Collection Algorithms: Mark-Sweep, Mark-Copy, Mark-Compact, Generational Collection
Four core algorithms plus the generational strategy used in production JVMs.
Mark-Sweep Algorithm
Core Idea : Mark live objects, then directly sweep dead objects.
Steps :
Mark Phase : Traverse all reachable objects from GC Roots and mark them (STW)
Sweep Phase : Scan heap, reclaim all unmarked objects
✅ Pros : Simplest implementation, no object movement, friendly to old generation.
❌ Cons : Produces massive memory fragmentation, large objects cannot allocate; STW time grows linearly with live object count.
💡 Analogy : Sweeping only floor trash without organizing desk items leaves gaps that can't fit large suitcases.
Mark-Copy Algorithm
Core Idea : Trade space for time, solve fragmentation.
Steps :
Divide available memory into two equal halves , use only one at a time
Mark live objects in current half (STW)
Copy all live objects sequentially to the other free half
Clear the entire used half in one go
✅ Pros : Zero fragmentation; allocation via pointer bump, extremely efficient.
❌ Cons : Only 50% memory utilization; more live objects = higher copy overhead.
💡 Analogy : Moving house — only take useful items, completely empty old house, new house is perfectly organized.
Mark-Compact Algorithm
Core Idea : Combine advantages of previous two — no wasted space, no fragmentation.
Steps :
Mark all live objects (STW)
Compact Phase : Move all live objects to one end of memory, tightly packed
Clear everything beyond the boundary
✅ Pros : No fragmentation; 100% memory utilization.
❌ Cons : Must move many objects, STW longer than mark-sweep.
💡 Analogy : Organizing a wardrobe — fold all clothes to the left side, right side completely empty, maximizing space.
Four Core Algorithms Comparison
Mark-Sweep : Core Steps → Mark → Sweep; Memory Utilization → 100%; Fragmentation → Heavy; STW Time → Medium; Applicable Scenario → Old Gen (many live objects)
Mark-Copy : Core Steps → Mark → Copy → Clear; Memory Utilization → 50% (original) / 90% (JVM optimized); Fragmentation → None; STW Time → Short (few live objects); Applicable Scenario → Young Gen (short-lived)
Mark-Compact : Core Steps → Mark → Compact → Sweep; Memory Utilization → 100%; Fragmentation → None; STW Time → Longest; Applicable Scenario → Old Gen (many live objects)
Generational Collection : Core Steps → Divide generations + combine above algorithms; Memory Utilization → Near 100%; Fragmentation → Minimal; STW Time → Overall optimal; Applicable Scenario → Modern JVM mainstream
Generational Collection Algorithm
This is the actual strategy used by all commercial JVMs (HotSpot, OpenJ9) , based on the Weak Generational Hypothesis :
Vast majority of objects die young
Objects surviving more GC cycles are harder to reclaim
Thus JVM splits heap into two generations:
🐣 Young Gen : Object Characteristics → Die fast, low survival rate; Algorithm Used → Mark-Copy ; Typical GC → Minor GC
🧓 Old Gen : Object Characteristics → Live long, high survival rate; Algorithm Used → Mark-Sweep / Mark-Compact ; Typical GC → Major / Full GC
Young Gen further splits into Eden + Survivor (From + To), copying only between Survivors, avoiding 50% waste.
Old Gen uses CMS (sweep-based), G1, ZGC etc., all optimizing mark-compact with parallel/concurrent techniques.
┌───────────────────────────────────────────────────┐
│ Heap Memory │
├───────────────────┬───────────────────────────────┤
│ Young Gen (1/3) │ Old Gen (2/3) │
│ ┌───────┬───┬───┐ │ │
│ │ Eden │ S0│ S1│ │ Mark-Sweep + Mark-Compact │
│ │ 80% │10%│10%│ │ (CMS uses Mark-Sweep, G1 mixed)│
│ └───────┴───┴───┘ │ │
│ Mark-Copy │ │
└───────────────────┴───────────────────────────────┘Key Details
Young Gen uses 8:1:1 ratio for Eden and two Survivors, solving original mark-copy's 50% waste
Each Minor GC collects Eden and one Survivor, copying survivors to the other empty Survivor
Objects surviving 15 Minor GCs (default) promote to Old Gen
One-Sentence Summary
Young Gen uses copying for speed; Old Gen uses compacting for space efficiency. Generational collection automatically pairs these algorithms, balancing throughput and latency.
Interview Bonus Points
All GC algorithms have STW — only duration differs; marking must pause mutator threads to avoid missed/incorrect marks
Modern GC improvements:
G1: Region-based generational, balances throughput and low latency
ZGC/Shenandoah: Concurrent compaction, STW ≤ 10ms
CMS Specialty : Uses mark-sweep; concurrent phases cannot move objects, falls back to Serial Old for compaction
Common Garbage Collectors: Serial, Parallel, CMS, G1, ZGC, Shenandoah — Comparison & Selection
Collector Evolution Timeline
JDK 1.3 → Serial GC (single-threaded)
JDK 1.4 → Parallel GC (multi-threaded, throughput-first)
JDK 1.5 → CMS GC (concurrent, low latency)
JDK 7u4 → G1 GC (region-based generational)
JDK 11 → ZGC (low latency milestone)
JDK 12 → Shenandoah GC (ultra-low latency)Pause Time ↑
High ● Serial / Parallel ← Ancient, simple brute force
Medium ● CMS ← Low latency pioneer, but floating garbage
Low ● G1 ← Garbage-First, predictable pauses
Ultra-low ● ZGC / Shenandoah ← Sub-millisecond, pause independent of heap size
Eras: JDK5 → JDK7 → JDK9(default) → JDK11/12+Left side: Focus on throughput , pauses may be seconds or tens of seconds
Right side: Focus on response time , pauses reduced to milliseconds
Core Characteristics of Each Collector
1. Serial GC
Mode : Single-threaded, STW
Algorithm : Young Gen copying, Old Gen mark-compact
Pros : Simple, efficient, low memory overhead, no thread interaction cost
Cons : Long STW, poor multi-core utilization
Use Cases : Client apps, single-core servers, small heaps (<100MB)
2. Parallel GC
Mode : Multi-threaded, still STW
Algorithm : Same as Serial, just multi-threaded
Pros : High throughput, fully utilizes multi-core CPU
Cons : STW still long, not latency-friendly
Use Cases : Background batch processing, big data, scientific computing
Note : JDK 8 default collector
3. CMS GC
Mode : Concurrent mark-sweep, low latency priority
Core Phases : Initial Mark (STW) → Concurrent Mark → Remark (STW) → Concurrent Sweep
Pros : Concurrent execution, drastically reduces STW
Cons :
High CPU consumption
Produces memory fragmentation
Concurrent mode failure falls back to Serial Old GC
Use Cases : Internet web apps, e-commerce requiring low response time
Note : Deprecated since JDK 9
4. G1 GC
Mode : Region-based generational, balances throughput and latency
Core Idea : Divide heap into equal-sized Regions, prioritize collecting Regions with most garbage
Core Phases : Initial Mark → Concurrent Mark → Final Mark → Live Data Counting & Evacuation
Pros :
Predictable pause time model
No fragmentation
Balances throughput and latency
Cons :
Higher memory overhead than CMS
May underperform CMS on small heaps
Use Cases : Large heaps (>4GB), apps needing throughput/latency balance
Note : JDK 9+ default collector
5. ZGC
Mode : Region-based, non-generational, colored pointers
Core Advantage : Max pause ≤ 10ms , independent of heap size
Pros :
Ultra-low latency, near imperceptible
Supports TB-level heaps
No fragmentation
Cons :
Throughput slightly lower than G1
Higher CPU requirements
Use Cases : Financial trading, real-time systems, microservice gateways requiring ultra-low latency
Note : Production-ready since JDK 15
6. Shenandoah GC
Mode : Region-based, non-generational, Brooks pointers
Core Advantage : Concurrent compaction , pause time independent of heap size
Pros :
Earlier concurrent compaction than ZGC
Extremely short pauses
Supports large heaps
Cons :
Throughput slightly lower than ZGC
Incomplete support in some JDK distributions
Use Cases : Similar to ZGC, ultra-low latency scenarios
Note : OpenJDK exclusive, not in Oracle JDK
Core Metrics Comparison
Serial : Threads → Single; Algorithm (Young/Old) → Copy / Mark-Compact; Main STW Phases → Full STW; Max Pause → Long; Throughput → Low; Memory Overhead → Very Low; Heap Size → <100MB; Concurrency → None; JDK Version → All
Parallel : Threads → Multi; Algorithm (Young/Old) → Copy / Mark-Compact; Main STW Phases → Full STW; Max Pause → Medium; Throughput → Highest; Memory Overhead → Low; Heap Size → <4GB; Concurrency → None; JDK Version → All
CMS : Threads → Multi; Algorithm (Young/Old) → Copy / Concurrent Mark-Sweep; Main STW Phases → Initial Mark, Remark; Max Pause → Short; Throughput → Medium; Memory Overhead → Medium; Heap Size → 2-8GB; Concurrency → High; JDK Version → JDK8 and earlier
G1 : Threads → Multi; Algorithm (Young/Old) → Copy / Concurrent Mark-Compact+Copy; Main STW Phases → Initial Mark, Final Mark; Max Pause → Predictable (10-200ms); Throughput → Medium-High; Memory Overhead → Medium-High; Heap Size → 4-64GB; Concurrency → Medium-High; JDK Version → JDK9+ Default
ZGC : Threads → Multi; Algorithm (Young/Old) → Mark-Compact (Concurrent); Main STW Phases → Mark Start, Mark End; Max Pause → Ultra-short (<10ms); Throughput → Medium; Memory Overhead → High; Heap Size → 8GB-TB; Concurrency → Very High; JDK Version → JDK15+ Production
Shenandoah : Threads → Multi; Algorithm (Young/Old) → Concurrent Mark-Compact; Main STW Phases → Initial Mark, Final Mark; Max Pause → Ultra-short (<10ms); Throughput → Medium-Low; Memory Overhead → High; Heap Size → 8GB-TB; Concurrency → Very High; JDK Version → JDK12+ (Non-Oracle)
📌 Memory Overhead Interpretation : ZGC's colored pointers need extra bits, using high bits of x64 address space — no heap overhead but relies on multi-mapping, slightly increasing physical memory. Shenandoah needs extra forwarding pointers.
Selection Decision Tree
Start
│
├─ Heap < 100MB? → Yes → Serial GC
│
├─ Highest throughput? → Yes → Parallel GC
│
├─ Heap < 4GB? → Yes → Parallel GC
│
├─ Heap 4-8GB? → Yes → G1 GC
│
├─ Heap > 8GB? → Yes
│ │
│ ├─ Latency requirement moderate? → Yes → G1 GC
│ │
│ └─ Latency requirement extreme? → Yes
│ │
│ ├─ Using Oracle JDK? → Yes → ZGC
│ │
│ └─ Using OpenJDK? → Yes → Shenandoah GC
│
└─ Legacy systems → May need CMS GCReal-world majority : Since JDK11/17, heaps 2-8GB use G1 safely; if GC logs show high pauses, switch to ZGC immediately.
Interview High-Frequency Points
CMS Drawbacks : Fragmentation, high CPU, concurrent mode failure
G1 Core Idea : Region partitioning, Mixed GC, pause prediction
ZGC Key Tech : Colored pointers, load barriers, non-generational
ZGC vs Shenandoah : ZGC uses colored pointers, Shenandoah uses Brooks pointers
JDK Default GCs :
JDK 8: Parallel GC
JDK 9-10: G1 GC
JDK 11+: G1 GC (default), ZGC/Shenandoah (optional)
"Difference between G1 Mixed GC and ZGC Concurrent Compaction?"
G1 only has partial concurrent phases; object evacuation still needs brief STW. ZGC uses load barriers for forwarding during relocation, almost no pause.
"Why does CMS have floating garbage?"
During concurrent marking, new garbage created by mutator threads cannot be marked in current cycle, must wait for next GC.
"When must Serial be used?"
Besides single-core/tiny heap, when CMS/G1 concurrent mode fails, JVM falls back to Serial Old for Full GC — another reason to avoid Full GC.
Summary & Recommendations
Mainstream selection strategy:
Most apps: Use JDK default G1 GC
Ultra-low latency: Upgrade to JDK 17 + ZGC
Only special scenarios need other collectors
G1 GC Internals: Region Partitioning, RSet, SATB, Mixed GC
G1 GC Overall Positioning
G1 (Garbage-First), introduced in JDK7, default in JDK9, is a server-side collector designed to:
Balance high throughput and low latency
Support very large heaps (tens to hundreds of GB)
Provide predictable pause times (user-configurable max pause)
Its biggest innovation: breaking traditional generational contiguous memory layout , adopting Region partitioning.
Region Partitioning Mechanism
Core Idea
G1 divides the entire Java heap into multiple equal-sized independent Regions . Region size determined at JVM startup, range 1MB~32MB , must be power of two.
Heap Layout (logical):
┌─────────────────────────────────────────┐
│ E │ S │ O │ H │ Free │ E │ O │ S │.. │
└─────────────────────────────────────────┘
Each cell is a Region, dynamically assigned rolesRegion Types
┌───────────────────────────────────────────────────────────┐
│ Java Heap (N Regions) │
├────────┬────────┬────────┬────────┬────────┬──────────────┤
│ Eden │ Eden │ Survivor│ Survivor│ Old │ Humongous │
│ Region │ Region │ Region │ Region │ Region │ Region │
└────────┴────────┴────────┴────────┴────────┴──────────────┘Eden Region : Young Gen Eden, holds newly created objects
Survivor Region : Young Gen Survivor, holds objects surviving one GC
Old Region : Old Gen, holds long-lived objects
Humongous Region : Large object area, dedicated to objects exceeding 50% of Region size
Key Advantages
Fragmentation greatly reduced : G1 reclaims by Region, can compact after reclamation
Controllable pauses : G1 prioritizes Regions with most garbage based on user-set max pause time
Flexible generational management : No contiguous memory needed; Young/Old are dynamic Region sets
RSet (Remembered Set)
Problem Solved
Cross-generation references: If an old object references a young object, Minor GC would need to scan entire old gen, causing long pauses.
RSet Working Principle
Problem : To reclaim an old Region, we must know which other Regions reference objects inside it, else we might incorrectly collect live objects. Scanning whole heap per Region? Single pause would be huge.
G1 Solution : Each Region maintains an RSet , recording "which cards in other Regions reference me" .
Card Table : Heap divided into 512-byte cards. On reference assignment, write barrier marks corresponding card dirty.
RSet aggregates dirty cards by source Region — an index: other Region → which of my cards are referenced.
💡 Analogy : You manage a warehouse 🏭, each Region is a shelf. RSet is a notebook on each shelf listing "Shelf A level 3, Shelf B level 7 have packages pointing to items on this shelf". During reclamation you only check those spots, not the whole warehouse.
┌─────────────┐ ┌─────────────┐
│ Region A │ │ Region B │
│ (Old Gen) │ │ (Young Gen) │
│ objA ───────┼─────►│ objB │
└─────────────┘ └─────────────┘
│ ▲
│ │
▼ │
┌─────────────┐ ┌─────────────┐
│ Region A │ │ Region B │
│ RSet │ │ RSet │
│ Empty │ │ Contains │
│ │ │ Region A's │
│ │ │ ref pointer │
└─────────────┘ └─────────────┘Key Characteristics
Space-for-time : RSet consumes ~5-10% heap memory
Write barrier implementation : Updates RSet when references change
Only records non-local references : Avoids duplication
SATB (Snapshot At The Beginning)
Initial Mark: STW, mark objects directly reachable from GC Roots
Concurrent Mark: Traverse object graph concurrently with mutator
Remark: STW, process reference changes recorded by SATB
Cleanup: STW, count live objects per Region, calculate reclamation value
Problem Solved
Object reference changes during concurrent marking: If a live object's reference is modified, it might be incorrectly marked as garbage.
SATB Core Idea
At start of concurrent marking , take a snapshot of entire heap. All objects alive in snapshot, plus objects allocated during marking, are considered live.
Implementation Mechanism
Core Mechanism: pre-write barrier
During concurrent marking, when modifying a reference field obj.field = new_value, G1 records the old value
old_value</strong> <strong>before assignment</strong> into a queue. Thus even if old reference is severed, marking thread still treats it as "live" and continues tracing.</p>
<p><strong>Why This Design?</strong></p>
<p>SATB guarantees: <strong>At end of concurrent marking, all objects alive at snapshot time are marked</strong>. May over-mark some "floating garbage" (actually dead but retained by snapshot), but never misses live objects — safe. Floating garbage collected next GC, harmless.</p>
<p>🧠 <strong>One-Liner</strong>: Rather over-mark than miss. Use write barrier snapshot for correctness.</p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_png/Tfibbr8BqxBa8nQGSJzQ5kG5aicdXH6geMcCYBW0eLgQJ25t55XZRsdsXZr5y2icSUg2V7pJU2icRGtPzy2P0SIicZFdJibhJ1OHQ5VvD5IjGxyaA/640?wx_fmt=png&from=appmsg" alt="SATB illustration"/></p>
<h3>Key Advantages</h3>
<ul>
<li><strong>Reduces Remark pause</strong>: No need to re-scan entire heap</li>
<li><strong>Ensures concurrent marking correctness</strong>: No live objects missed</li>
</ul>
<h3>Mixed GC</h3>
<p>G1 GC modes:</p>
<ul>
<li><strong>Young GC</strong>: Only collects Eden and Survivor, similar to ParNew, STW</li>
<li><strong>Mixed GC</strong>: G1's signature — <strong>after concurrent marking, collects all young Regions AND a subset of "most cost-effective" old Regions</strong></li>
<li><strong>Full GC</strong>: Fallback to single-threaded Serial GC when overwhelmed — avoid at all costs</li>
</ul>
<h3>What Is Mixed GC</h3>
<p>Mixed GC is unique to G1: it <strong>recycles all young Regions plus a portion of old Regions with most garbage</strong>.</p>
<h3>Trigger Timing</h3>
<p>When old gen occupancy reaches <strong>IHOP (Initiating Heap Occupancy Percent)</strong> threshold, concurrent marking cycle starts, followed by multiple Mixed GCs.</p>
<h3>Execution Flow</h3>
<pre><code>1. Initial Mark (STW) → 2. Concurrent Mark → 3. Remark (STW) → 4. Cleanup (STW)
↓
5. Multiple Mixed GCsTypical G1 Heap Cycle Diagram
gantt
title G1 Mixed GC Rhythm
dateFormat HH:mm
axisFormat %H:%M
section GC Phases
Concurrent Mark :a1, 00:00, 10m
Remark :crit, a2, after a1, 0.1m
Cleanup :crit, a3, after a2, 0.1m
Mixed GC ① :crit, m1, after a3, 0.05m
Mixed GC ② :crit, m2, after m1, 0.05m
Mixed GC ③ :crit, m3, after m2, 0.05mThis design achieves the pause target: Each cycle reclaims only as many old Regions as pause budget allows, never biting off more than it can chew , keeping service stable and smooth 🧈.
Key Characteristics
Executed in multiple rounds : Avoids long pauses from reclaiming too many old Regions at once
Prioritizes Regions with most garbage : Origin of "Garbage-First" name
Controllable pauses : Number of Regions reclaimed per cycle determined by user-set max pause time
Interview Bonus Summary
G1's core innovations: Region partitioning and prioritizing Regions with most garbage
RSet solves cross-generation reference scanning via write barriers
SATB solves concurrent marking correctness via snapshot concept
Mixed GC is G1-specific, collects both young and old generations together
G1's greatest advantage: predictable pause times , suitable for large heaps
ZGC Internals: Colored Pointers, Concurrent Compaction, Sub-millisecond Pauses
Colored Pointers
ZGC's most innovative design, foundation of all concurrent operations.
Core Principle
Traditional GC stores metadata (marking info, forwarding info) in object headers . ZGC stores them directly in the high 4 bits of 64-bit pointers . Modern CPUs use only low 48 bits for virtual addresses; high 16 bits are free. ZGC "steals" 4 bits for "color marking".
|←── 16 unused bits ──|← 4 color bits →|←──── 44-bit physical address ────→|Color bits (on Linux only low 2 bits used) indicate object's current "view":
Remapped (00) — Object relocated, points to latest location
Marked0 / Marked1 (01/10) — Dual marking bits alternated to prevent concurrent marking residue
Question: Color occupies bits, how to dereference? Enter ZGC's black tech — Multi-mapping . OS maps same physical memory to multiple virtual address ranges (remapped view, marked0 view, marked1 view). Different high bits directly hit corresponding view. Memory layout:
Physical Memory Virtual Address Space (Views)
┌─────────────┐ remapped region: 0x0000... → actual addr + 00
│ Object Data │ marked0 region: 0x0010... → actual addr + 01
│ Object Data │ marked1 region: 0x0020... → actual addr + 10
└─────────────┘Thus reading a colored pointer requires no extra bit-mapping; high bits directly select view , extremely efficient.
Four Color Bits Meaning
Bit 0 : Marked0 — Mark phase 0 liveness mark
Bit 1 : Marked1 — Mark phase 1 liveness mark
Bit 2 : Remapped — Object relocation completed mark
Bit 3 : Finalizable — Object needs finalize() execution
Major Advantages
No object header modification : All metadata ops on pointers, avoiding concurrent header modification contention
Low memory overhead : Each object saves 8-byte mark word
Enables concurrent operations : Pointer color reveals object state, no STW needed
Concurrent Compaction
ZGC's solution to memory fragmentation , its biggest advantage over G1. G1 only compacts during Mixed GC with STW; ZGC uses "Forwarding Table + Load Barrier Self-Healing" for fully concurrent compaction.
Traditional GC Pain Point
Mark-compact requires STW because object addresses change during compaction; if mutator threads run concurrently, they'd access wrong addresses.
ZGC Solution: Load Barrier + Colored Pointers
On every object reference read , ZGC inserts a load barrier checking pointer color:
If pointer is Remapped: Directly access object
If pointer is Forwarded: Automatically jump to new address, update pointer to Remapped state
Three Phases of Concurrent Compaction
Concurrent Mark : GC and mutator threads run concurrently, mark all live objects
Concurrent Relocation : GC copies live objects to new Regions, generates forwarding table
Concurrent Remapping : Mutator threads automatically update all pointers to old addresses via load barriers
Flow Overview
[Mark Live] → [Select Regions to Clean] → [Concurrent Copy + Self-Heal Forwarding] → [Release Old Regions]
↑ ↑
│ Load barriers auto-fix pointers (self-heal)
└───── All concurrent with mutator, only tiny STW at start/end ──────────┘Sub-millisecond Pauses
ZGC achieves sub-millisecond pauses, independent of heap size, because nearly all GC work is concurrent.
Only STW Phases
Initial Mark : Only scans GC Roots, time proportional to GC Root count (typically few ms)
Final Mark : Processes incremental updates from concurrent marking, extremely short (usually <1ms)
Why Sub-millisecond?
No object header modification lock contention (state in pointers)
No global heap freeze for compaction (concurrent relocation)
Load barrier self-healing avoids secondary tracing pauses during concurrent relocation
Combined with colored pointers' multi-view, even determining whether object is in marking or relocation is branchless code , pauses shrink to lightning. Real-world: 16 TB heap, trillions of references, max pause still under 1 ms. 🌩️
Key Data Comparison
Parallel GC : Typical Pause → Hundreds of ms to seconds; Pause vs Heap Size → Positive correlation
G1 : Typical Pause → Tens to hundreds of ms; Pause vs Heap Size → Weak correlation
ZGC : Typical Pause → <10ms; Pause vs Heap Size → Completely independent
Why Pause Independent of Heap Size?
Because ZGC's STW phases only process GC Roots, whose count is fixed and doesn't grow with heap. Even expanding heap from 8GB to 1TB, pause remains essentially constant.
Summary
🎨 Colored Pointers : Role → State in pointers, multi-view mapping zero-cost dereference; Effect → Replaces object header, enables concurrent state transitions
🧹 Concurrent Compaction : Role → Copy objects while mutators read/write, load barriers self-heal; Effect → Move objects without stopping business
⏱️ Ultra-short STW : Role → Only root scanning, minimal convergence, root redirection pauses; Effect → Sub-millisecond, heap-size independent
ZGC leverages colored pointers to cleverly use free bits in 64-bit pointers for metadata, combined with load barriers to achieve fully concurrent memory compaction , ultimately delivering sub-millisecond pauses . Ideal for latency-sensitive apps: financial trading, real-time communication, game servers.
GC Tuning Methodology & Common Parameters
Three Questions Before Tuning
What's the goal? High throughput or low latency? Often mutually exclusive.
What's the symptom? Frequent Young GC, or Mixed/Full GC that won't stop?
What's the root cause? Allocation rate too high, or objects prematurely promoted to old gen?
Without these, tuning is blind.
Ultimate Goal of GC Tuning
GC tuning solves business pain points . Priority order:
Availability first : Avoid OOM, frequent Full GC causing crashes
Latency first : Response-time sensitive businesses (e-commerce flash sales, payments)
Throughput first : Background batch, big data computing
Memory footprint : Containerized deployments, resource-constrained environments
Core Monitoring Metrics
Minor GC Duration : Normal Reference → < 50ms; Alert Threshold → > 100ms needs attention
Full GC Frequency : Normal Reference → Once per few hours or none; Alert Threshold → Multiple within 1 hour, severe
Full GC Avg Duration : Normal Reference → < 1s; Alert Threshold → > 2s impacts availability
Post-Young-GC Live Objects : Normal Reference → Usually very low; Alert Threshold → High suggests premature promotion
Old Gen Occupancy Growth Rate : Normal Reference → Gradual; Alert Threshold → Sharp rise indicates leak or promotion anomaly
Feed -Xlog:gc* (JDK9+) or -XX:+PrintGCDetails logs into GCeasy etc., these metrics become obvious.
General Tuning Logic (Flow)
Golden 6 Steps of GC Tuning
1. Problem Identification: Find Root Cause First
Symptoms : API timeouts, CPU spikes, service stalls, OOM crashes
Tools : jstat -gcutil pid 1000 (real-time), jmap -dump (heap dump), Arthas, GCEasy, MAT
Key Metrics :
Full GC frequency & duration (>1/hr and >1s → investigate)
Young GC frequency & duration
Old gen usage (>80% and rising)
Metaspace usage
2. Define Target: Quantify Tuning Goals
Bad example: "Make GC faster"
Good example: "Reduce Full GC frequency from 1/10min to 1/day, single Full GC duration under 500ms"
3. Choose Right Collector (Most Critical Step)
G1 : Applicable Scenario → General purpose, 4-64GB heap; Core Advantage → Predictable pauses, balances throughput/latency; Biggest Weakness → Small heaps (<4GB) underperform Parallel; JDK Version → JDK9+ Default
ZGC : Applicable Scenario → Large memory (8GB+), low latency; Core Advantage → Pause <10ms, heap-size independent; Biggest Weakness → Throughput slightly lower than G1; JDK Version → JDK15+ Production
Shenandoah : Applicable Scenario → Low latency, similar to ZGC; Core Advantage → Pause <10ms, open source; Biggest Weakness → Not in some JDK distributions; JDK Version → JDK12+
Parallel : Applicable Scenario → High throughput, background tasks; Core Advantage → Highest throughput; Biggest Weakness → Uncontrollable pauses; JDK Version → JDK8 Default
CMS : Applicable Scenario → Low latency (deprecated); Core Advantage → Concurrent mark, short pauses; Biggest Weakness → Fragmentation, high CPU; JDK Version → JDK9 Deprecated
💡 Interview Must-Know : JDK8 → Parallel, JDK11+ → G1, JDK17+ → ZGC directly — industry best practice.
4. Memory Allocation: Size First, Ratios Later
Heap Size : -Xms and -Xmx must be equal to avoid dynamic resizing overhead
Young Gen Size : G1 discourages manual -Xmn; let G1 auto-adjust. Parallel: Young Gen 1/3 to 1/2 of heap
Metaspace Size : -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m, prevent Full GC triggered by metaspace expansion
5. Parameter Tuning: Targeted Optimization, No Blind Copying
Tune collector and heap size first, then other parameters
Change one parameter at a time for clear comparison
Keep tuning records for rollback
6. Verify & Iterate: Load Test + Production Monitoring
Use JMeter, Gatling for load testing simulating real traffic
Continuously monitor GC metrics post-deployment
No perfect parameters, only most suitable for your business
Common GC Tuning Parameters Cheat Sheet
General Parameters
-Xms4g -Xmx4g: Function → Initial and max heap size; Recommended Value → 1/2 to 2/3 of physical memory -XX:+UseG1GC: Function → Use G1 collector; Recommended Value → JDK9+ Default -XX:+UseZGC: Function → Use ZGC collector; Recommended Value → JDK17+ Recommended -XX:+PrintGCDetails: Function → Print detailed GC logs; Recommended Value → Enable in production -Xloggc:/var/log/gc.log: Function → GC log output path; Recommended Value → Daily rotation recommended -XX:+HeapDumpOnOutOfMemoryError: Function → Auto heap dump on OOM; Recommended Value → Must enable -XX:HeapDumpPath=/var/log/heapdump.hprof: Function → Heap dump file path; Recommended Value → Ensure disk space
G1-Specific Parameters
-XX:MaxGCPauseMillis=200: Function → Max pause time target; Recommended Value → 100-500ms, adjust per business -XX:G1HeapRegionSize=16m: Function → Region size; Recommended Value → 1-32m, larger heap → larger Region -XX:InitiatingHeapOccupancyPercent=45: Function → Heap occupancy triggering concurrent mark; Recommended Value → 35-45% -XX:G1NewSizePercent=5: Function → Young gen minimum percentage; Recommended Value → 5% -XX:G1MaxNewSizePercent=60: Function → Young gen maximum percentage; Recommended Value → 60%
ZGC-Specific Parameters
-XX:+UnlockExperimentalVMOptions: Function → Unlock experimental options; Recommended Value → Required for JDK11-14 -XX:ZCollectionInterval=300: Function → Force GC interval (seconds); Recommended Value → 300-600s -XX:ZAllocationSpikeTolerance=2: Function → Allocation spike tolerance; Recommended Value → 2
Logging & Diagnostics
# JDK9+
-Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=10,filesize=100m
# JDK8
-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log
# OOM Auto Dump
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dump/Interview Bonus: Common Pitfalls & Practical Experience
Pitfall 1: Larger Young Gen Always Better
Too large → Young GC duration increases; too small → objects prematurely enter old gen, triggering Full GC.
Pitfall 2: More Parameters = Better
Most parameters should stay default; JVM team has done extensive optimization.
Pitfall 3: One-Time Tuning Forever
Business changes, traffic changes — GC tuning is continuous iteration.
Practical Experience
On OOM, immediately preserve heap dump — key to root cause analysis
Container deployments: watch for JVM container memory awareness; JDK8u191+ supports it
Large objects directly entering old gen fills old gen fast — avoid creating large objects
Throughput → Parallel , batch processing no hesitation
Low latency → G1 , set pause target conservatively
Ultra-low latency → ZGC , heaps <8GB less meaningful
Don't tune JVM first — check code for issues: infinite loops creating objects, unbounded caches
One parameter at a time , change → load test → observe effect, else you won't know what worked
Summary
GC tuning core is "Identify problem first, then solve" . 90% of GC issues resolved by "Right collector + proper memory sizing" ; remaining 10% need deep parameter tuning. Remember: Code optimization > GC tuning . Good code is best GC tuning: avoid memory leaks, reduce object allocation, use object pools. Set Goal → Capture Logs → Analyze Distribution → Adjust Parameters → Verify , repeat. Tools: jstat for quick view, GCViewer/GCeasy for deep analysis, MAT/JProfiler for leak detection.
JDK 17 Default GraalVM JIT Compiler Optimizations
Clarify a high-frequency interview misconception : JDK 17 itself does not set GraalVM JIT as default compiler. However, Oracle JDK 17 includes Graal Compiler as an experimental production-grade JIT enabled via JVM flags; GraalVM CE 17 (built on OpenJDK 17) uses Graal JIT by default. Standard OpenJDK 17 requires -XX:+UseJVMCICompiler to enable manually. Below covers core optimizations, performance comparisons, and applicable scenarios.
Why Graal JIT Called Next-Gen Compiler?
Architecture comparison:
┌─────────────────────────────────────────────────────────┐
│ HotSpot JVM Tiered Compilation │
├─────────────────────────────────────────────────────────┤
│ Level 0 │ Pure Interpretation (Interpreter) │
│ Level 1-3│ C1 Compiler (Client Compiler) — Fast compile, light optimization │
│ Level 4 │ Top-Tier JIT │
│ │ ┌──────────────┬──────────────────┐ │
│ │ │ Traditional C2 │ Graal JIT │ │
│ │ │ C++ Implementation│ Java Implementation│ │
│ │ │ 20-Year History │ Latecomer Advantage│ │
│ │ │ Graph IR (HIR) │ Graph IR (HIR) More Flexible│
│ │ └──────────────┴──────────────────┘ │
└─────────────────────────────────────────────────────────┘5 Core Graal JIT Optimizations vs C2
Graal JIT written entirely in Java (C2 in C++), uses advanced Sea-of-Nodes IR, surpasses C2 in global optimization:
Escape Analysis : C2 → Only Full Escape Analysis ; Graal JIT → Supports Partial Escape Analysis (PEA) (Killer optimization); Actual Performance Gain → Temporary object allocation reduced 30-70%, GC pressure drastically lower 🚀
Inlining : C2 → Basic inlining, limited for complex methods; Graal JIT → Smart inlining, supports Lambda/Stream deep inlining; Actual Performance Gain → Method call overhead reduced 20-40%
Devirtualization : C2 → Basic devirtualization; Graal JIT → Aggressive whole-program devirtualization; Actual Performance Gain → Virtual call overhead reduced 50%+
Loop Vectorization : C2 → Basic SIMD support; Graal JIT → Advanced auto-vectorization, supports AVX-512 etc.; Actual Performance Gain → Numeric computation speedup 10-30%
Global Optimization : C2 → Traditional IR, limited scope; Graal JIT → Sea-of-Nodes IR, cross-function global optimization; Actual Performance Gain → Overall code quality improvement 5-15%
Partial Escape Analysis (PEA) — Key Point!
Graal's flagship optimization, illustrated with code:
// Scenario: Stream + Lambda high-frequency code
list.stream()
.filter(x -> x > 10)
.map(x -> new Result(x * 2)) // ← new object per element
.collect(Collectors.toList());C2's Dilemma : Sees Result object may escape (collected into List), refuses to optimize — every element heap-allocated. Graal's Approach :
x=5 ❌ filtered out → doesn't enter map → no Result allocation 🎉
x=15 ✅ → map → new Result(30) → escapes to List → forced heap allocation
x=20 ✅ → map → new Result(40) → escapes to List → forced heap allocation
→ Only 2/3 objects went to heap, 1/3 less GC pressure than C2!📊 Effect Comparison:
Object Allocations GC Pause Frequency
┌─────────┐ ┌─────────┐
│ C2: 300k│ │ C2: 15 │
│ Graal:200k│ ✅ 33% less │ Graal:10│ ✅ 33% less
└─────────┘ └─────────┘Graal docs explicitly state: Modern Java code using Streams/Lambdas benefits most from this optimization.
Method Inlining Upgrade
Graal's inlining more aggressive:
Deep Inlining : Multi-layer functional chains ( .filter().map().collect()) "flattened" into single compilation unit
Polymorphic Inlining : For virtual calls, Graal inlines multiple implementation branches based on runtime profile
Reflective Call Inlining : Reflection-heavy scenarios (JSON serialization) — Graal identifies hot reflective calls and inlines, eliminating reflection overhead
🎤 Interview Talking Point : "Graal's optimization of highly abstract code makes it especially suitable for functional programming and microservice frameworks (Spring Boot, Quarkus), covering most modern Java app performance bottlenecks."
libgraal: JIT Compiler Also AOT Compiled?
Perhaps Graal's most underestimated design. Architecture:
Normal JIT Startup (jar mode):
┌──────────┐ ┌───────────┐ ┌──────────────┐
│ Start JVM│→ │ Interpret │→ │ JIT Self-Boot │→ Start Compiling Hot Code
│ │ │ User Code │ │ (Slow!) │
└──────────┘ └───────────┘ └──────────────┘
↑ May take minutes!
Graal libgraal Mode (default):
┌──────────┐ ┌───────────────────┐ ┌──────────────┐
│ Start JVM│→ │ libgraal.so Ready │→ │ Immediate │
│ │ │ (AOT Compiled) │ │ Compile Hot │
└──────────┘ └───────────────────┘ └──────────────┘
↑ Resides in HotSpot Off-Heap!libgraal : Graal compiler itself AOT compiled to native shared library ( .so/.dylib), loaded directly into JVM, zero warm-up startup
Uses independent memory space, doesn't consume Java heap, less GC pressure
Huge advantage in cloud-native and Serverless: function instances start at peak performance immediately.
Graal vs C2: How to Choose?
Performance Heatmap (darker = Graal advantage more pronounced)
┌─────────────────────┬──────────┬──────────┐
│ Scenario │ C2 │ Graal │
├─────────────────────┼──────────┼──────────┤
│ Traditional OOP Biz │ ████████ │ ████████ │ ≈ Parity
│ Functional/Stream │ ██████ │ ████████ │ ✅ Graal Stronger
│ Reflection-heavy │ ██████ │ ████████ │ ✅ Graal Stronger
│ Dynamic Languages │ ██████ │ ████████ │ ✅ Graal Stronger
│ Compute-intensive │ ████████ │ ████████ │ ≈ Parity
│ Startup Speed │ ████████ │ ████████ │ ≈ Parity (libgraal)
└─────────────────────┴──────────┴──────────┘
💡 Key Insight: Graal doesn't universally crush C2; 10-20% edge in specific scenarios.
Benchmarks show overall slight advantage but gap not huge.Selection Guide
Spring Boot Microservices + Stream/Lambda → 🟢 GraalVM
Traditional Java EE / Heavy Sync Business Logic → 🔵 OpenJDK + C2 Sufficient
Need Native Image Deployment → 🟢 GraalVM Mandatory
Kotlin/Scala etc. JVM Languages → 🟢 Graal Better for Functional Code
Real-World Performance Comparison
Based on OpenJDK 17 benchmarks (C2 = 100, higher better): Key Conclusions:
Lambda/Stream-heavy code benefits most (35% boost) — most common modern Java scenario
Traditional enterprise apps parity or slight gain (8%), no regression
Combined with GraalVM AOT, microservice startup speed improves 50-90%
Applicable Scenarios & Limitations
✅ Best Fit Scenarios
Numeric, scientific computing, ML inference
Big data processing (Spark, Flink)
Microservices, Serverless functions
Modern Java code (heavy Lambda, Stream, Optional)
Cross-language interop (with GraalVM polyglot)
❌ Not Recommended
Extreme startup requirements without AOT
Embedded devices with <512MB memory
Legacy code relying on undocumented C2-specific optimizations
Compilation-time-sensitive short-lived programs
Interview Bonus Knowledge Points
Enable Flags : JDK 17 requires -XX:+UnlockExperimentalVMOptions -XX:+UseGraalJIT Tiered Compilation : Graal JIT works with C1 forming "Interpret → C1 → Graal" three tiers, balancing startup and peak performance
Compilation Overhead : Graal JIT compile time 20-50% longer than C2, memory 10-20% higher — better for long-lived apps
Future Trend : Since JDK 21, Graal JIT becomes default on some platforms, will gradually replace C2 entirely
One-sentence summary of Graal JIT core value: 🔥 Graal is a Java-written, graph-IR-based aggressive JIT compiler; its killer feature is Partial Escape Analysis — fine-grained elimination of useless object allocations in Stream/Lambda scenarios; combined with libgraal zero-warmup startup, it's tailor-made for cloud-native microservice architectures. Three memory anchors:
PEA : Partial Escape Analysis — "Allocate objects only on escaping paths"
libgraal : AOT-compiled compiler native library — "Compiler itself zero warm-up"
Graal IR : Graph Intermediate Representation — "Language-agnostic, more flexible optimization"
Common Interview Traps
❌ Wrong : "JDK 17 defaults to Graal JIT" → ✅ Correct : Need GraalVM distribution for default; standard OpenJDK needs manual enable
❌ Wrong : "Graal JIT crushes C2 everywhere" → ✅ Correct : Scenario-dependent; traditional biz code gap small
❌ Wrong : "Graal is only for AOT Native Image" → ✅ Correct : JIT and AOT are both core Graal modes — don't confuse
❌ Wrong : "Enabling Graal JIT slows startup" → ✅ Correct : libgraal mode pre-compiles compiler as native lib, startup overhead nearly zero
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.
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.
