CollectedHeap class.
Available Collectors
HotSpot provides five production-ready garbage collectors, all located insrc/hotspot/share/gc/:
Serial GC
Single-threaded collector for small applications
Parallel GC
Throughput-focused multi-threaded collector
G1 GC
Region-based low-latency collector (default)
ZGC
Scalable ultra-low latency collector
Shenandoah
Concurrent low-pause collector
Epsilon
No-op collector for testing (no actual collection)
G1 Garbage Collector
The Garbage-First (G1) collector is the default in modern JDK releases. Implementation:src/hotspot/share/gc/g1/
Architecture
Fromg1CollectedHeap.hpp:
“A G1CollectedHeap is an implementation of a java heap for HotSpot. It uses the Garbage First heap organization and algorithm, which may combine concurrent marking with parallel, incremental compaction of heap subsets that will yield large amounts of garbage.”
Region-Based Heap
G1 divides the heap into fixed-size regions (1-32MB):- Eden regions - Young generation allocation targets
- Survivor regions - Objects that survived one GC
- Old regions - Long-lived objects
- Humongous regions - Large objects (> 50% of region size)
Regions allow G1 to collect portions of the heap incrementally, meeting pause time goals while maximizing throughput.
Key Components
The G1 implementation includes:G1HeapRegionManager - Region allocation and tracking
G1CollectionSet - Regions selected for collection
G1RemSet - Remembered set for cross-region references
G1ConcurrentRefine - Concurrent refinement of card tables
Collection Phases
- Young Collection
- Concurrent Marking
- Mixed Collection
Collects Eden and Survivor regions:
- Root Scanning - Scan thread stacks, static fields, JNI
- Update RS - Process dirty cards from concurrent refinement
- Scan RS - Scan remembered sets for cross-region references
- Object Copy - Evacuate live objects to survivor/old regions
- Reference Processing - Handle weak/soft/phantom references
- Region Cleanup - Reclaim empty regions
Card Tables and Remembered Sets
G1 uses card tables (g1CardTable.hpp) and card sets (g1CardSet.hpp) to track cross-region references:
- Heap divided into 512-byte cards
- Modified cards tracked in per-region remembered sets
- Concurrent refinement threads process cards asynchronously
- Reduces young GC scanning to only relevant old regions
Z Garbage Collector
ZGC is a scalable, low-latency garbage collector introduced in Java 11. Implementation:src/hotspot/share/gc/z/
Design Goals
FromzCollectedHeap.hpp:
- Sub-millisecond pause times - Maximum 1ms pauses
- Terabyte+ heaps - Scales to multi-TB heap sizes
- Minimal throughput overhead - < 15% vs. Parallel GC
Colored Pointers
ZGC uses pointer metadata (colored pointers) to track object state:- Marked0/Marked1 - Concurrent marking state
- Remapped - Whether pointer has been updated
- Finalizable - Object has finalizer
Colored pointers enable concurrent relocation without read barriers, using load barriers instead to heal references on access.
Load Barriers
ZGC uses load barriers to maintain correctness during concurrent operations:Collection Phases
ZGC performs most work concurrently:- Pause Mark Start (< 1ms) - Scan thread roots
- Concurrent Mark - Traverse object graph
- Pause Mark End (< 1ms) - Finalize marking
- Concurrent Reference Processing - Process weak references
- Concurrent Relocation Preparation - Select pages to relocate
- Pause Relocate Start (< 1ms) - Relocate roots
- Concurrent Relocation - Move objects, remap references
Generational ZGC
Recent versions include generational support (_driver_minor / _driver_major):
- Young generation for recently allocated objects
- Old generation for long-lived objects
- Reduces marking overhead for stable heaps
- Further improves throughput
Shenandoah Collector
Shenandoah is a low-pause collector developed by Red Hat. Implementation:src/hotspot/share/gc/shenandoah/
Architecture
FromshenandoahHeap.hpp:
Forwarding Pointers
Shenandoah uses a different approach than ZGC:- Brooks pointers - Indirection pointer at object start
- Objects have a forwarding pointer field
- During relocation, pointers updated to new location
- Read barriers check and follow forwarding pointers
Liveness Data
From the source comments:Collection Modes
- SATB Mode
- IU Mode
- Passive Mode
Snapshot-At-The-Beginning concurrent marking:
- Similar to G1’s SATB approach
- Concurrent mark from snapshot
- Write barriers maintain SATB invariant
- Default and most mature mode
Evacuation and Pacing
Key differentiators:- Concurrent Evacuation - Moves objects while application runs
- Allocation Pacing - Throttles allocation when GC can’t keep up
- Generational Support - Young and old generations (
ShenandoahYoungGeneration,ShenandoahOldGeneration)
Serial Collector
The simplest collector, using single-threaded mark-compact algorithm. Located insrc/hotspot/share/gc/serial/.
Use Cases:
- Single-CPU systems
- Small heaps (< 100MB)
- Batch jobs where pause time doesn’t matter
- Minimal memory footprint required
- Mark live objects from roots
- Compute forwarding addresses
- Update all references
- Move objects to compacted locations
Parallel Collector
Multi-threaded throughput-oriented collector. Located insrc/hotspot/share/gc/parallel/.
Generations:
- Young generation - Parallel Scavenge
- Old generation - Parallel Mark-Compact
- Maximum throughput prioritized over latency
- Adaptive sizing based on throughput/pause goals
- Excellent for batch processing and computations
- Longer pause times than G1/ZGC/Shenandoah
- Better throughput on multi-core systems
- Simpler than region-based collectors
Epsilon Collector
A no-op collector that never reclaims memory. Located insrc/hotspot/share/gc/epsilon/.
Purpose:
- Performance testing and benchmarking
- Ultra-short-lived applications
- Measuring GC overhead
- Testing GC interface contracts
Epsilon will eventually run out of memory. It’s only suitable for testing or applications with known bounded allocation.
Shared GC Infrastructure
All collectors use common infrastructure fromsrc/hotspot/share/gc/shared/:
Barrier Sets
Memory barriers for maintaining GC invariants:CardTableBarrierSet- Card marking for generational collectorsG1BarrierSet- SATB and card marking for G1ShenandoahBarrierSet- Brooks pointer barriersZBarrierSet- Load barriers for ZGC
Work Queues
Parallel GC work distribution:Reference Processing
Handling of weak/soft/phantom references:- Discovered during marking
- Processed at end of GC cycle
- Notifications enqueued for application
- Implemented in
referenceProcessor.hpp
Selecting a Collector
| Collector | Flag | Best For | Pause Time | Throughput |
|---|---|---|---|---|
| G1 | -XX:+UseG1GC | General purpose, balanced | 10-200ms | Good |
| ZGC | -XX:+UseZGC | Large heaps, low latency | < 1ms | Good |
| Shenandoah | -XX:+UseShenandoahGC | Low latency | < 10ms | Good |
| Parallel | -XX:+UseParallelGC | Throughput, batch | 100ms-10s | Excellent |
| Serial | -XX:+UseSerialGC | Small heaps, single CPU | 10ms-1s | Poor |
Next Steps
HotSpot VM
Return to VM architecture overview
JIT Compiler
Explore JIT compilation system