Choosing a Java Garbage Collector: Which Metric Are You Optimizing For?
This guide explains Java garbage collector internals and selection criteria, covering Serial, Parallel, G1, ZGC, and Shenandoah, detailing their trade-offs between throughput, latency, and memory overhead, and provides scenario-based recommendations for microservices, low-latency systems, batch processing, and small containers, including upcoming Java 27 defaults.
1. GC Fundamentals
1.1 GC Manages Objects
Garbage collection is not just about freeing memory; it manages the lifecycle of objects — allocation and reclamation. A simple Person class with an int field and a String reference illustrates object layout in the heap: each instance has an object header (class metadata pointer, GC age, lock bits, identity hash), inline primitive fields, and reference fields pointing to other objects (e.g., String → byte[]). The object header is the prerequisite for all GC mechanisms.
1.2 Why Generational Heap?
Early JVMs used mark-sweep on the whole heap, causing two fatal problems: memory fragmentation (free memory exists but not contiguously) and linearly growing pause times as the heap grows. In 1998, generational collection was introduced based on the weak generational hypothesis : most objects die young. The heap splits into Young (Eden + two Survivor spaces) and Old generations. New objects allocate in Eden; after a minor GC, survivors are copied to a Survivor space, their GC age increments, and once it exceeds a threshold (typically 10–15) they are promoted to Old. Minor GC uses mark-copy-sweep on Eden and Survivor: copy live objects, then wipe the entire region at once — no per-object handling, no fragmentation. Long-lived old objects avoid repeated scanning.
1.3 Cross-Generation References
If an old-generation object references a young object, the young object must not be collected, but scanning the entire old generation defeats generational isolation. The JVM solves this with a Card Table + Write Barrier . The old generation is divided into 512-byte cards; a byte array (card table) marks cards containing old-to-young references. A write barrier — a tiny code snippet executed on every reference assignment — sets the card table entry:
if (source is old && target is young) cardTable[address >> 9] = 1;(right-shift 9 bits = divide by 512). During minor GC, only marked cards are treated as additional GC roots, trading a tiny write-time cost for a large read-time scan reduction.
2. Serial and Parallel GC
Serial GC uses a single thread for all GC work; all application threads stop (STW). It has the lowest CPU and memory overhead, making it the default for machines with <2 CPUs or <2 GB heap (e.g., small microservices). Parallel GC parallelizes the same work across multiple threads, saturating CPU cores. It remains STW, so large heaps can cause second-long pauses, but it maximizes throughput — the proportion of CPU time spent on application vs. GC. Parallel suits batch processing, data computation, and queue consumers where total efficiency matters and no user waits. However, for user-facing services, multi-second pauses are disastrous: users retry, amplifying load. Thus pause time becomes a critical metric.
3. From CMS to G1
CMS (Concurrent Mark Sweep) attempted concurrent marking and sweeping to reduce pauses, but its sweep phase does not compact, so fragmentation returned. G1 (Garbage-First) , introduced in Java 7 and default since Java 9, partitions the heap into 2,000–4,000 equal-sized Regions . Each region dynamically acts as Eden, Survivor, or Old. Objects larger than half a region go into Humongous Regions (spanning multiple regions if needed) because moving them is too expensive. G1's core cost center is the Remembered Set (RSet) : each region maintains an RSet with its own card table, tracking incoming references from other regions so it need not scan the whole region. Most G1 optimizations since Java 7 target RSet efficiency. The "Garbage-First" name comes from its collection policy: a concurrent task continuously estimates garbage ratio in old regions; during GC, G1 picks regions with the most garbage and fewest live objects first, maximizing reclamation per copied byte. Collection pauses are still STW, but G1 chooses how many regions to collect to meet a pause target (default 200 ms). Exception: if memory is critically low, G1 falls back to a full-heap STW Full GC — frequent G1 Full GCs indicate configuration or allocation problems, not a G1 flaw.
4. ZGC and Shenandoah: Ultra-Low Latency
Even 200 ms pauses are too high for some systems. ZGC and Shenandoah achieve sub-millisecond pauses, largely independent of heap size, by performing marking, relocation, and pointer update concurrently with application threads . Objects move while the application runs, requiring a Load Barrier (Read Barrier) : on every reference read, the thread checks if the address is current and fixes it if not. The barrier must be extremely short.
Their difference lies in where GC metadata lives:
ZGC uses Colored Pointers : GC state is encoded in the high bits of 64-bit pointers (low 42 bits address up to 16 TB). A side effect: the same physical memory is counted multiple times, which confused Kubernetes OOM killers in early non-generational ZGC. Generational ZGC (now the only mode; non-generational removed in Java 24) expands colors from 4 to 12, keeps the barrier at two assembly instructions, extends addressing to 64 TB, and fixes memory reporting. It is nearly self-tuning. Trade-off: because metadata lives in pointer high bits, ZGC cannot use compressed oops — it requires full 64-bit references. If GC threads fall behind allocation, Allocation Stalls occur — the key metric to monitor.
Shenandoah stores metadata in the object header : two tag bits indicate an object is being processed; when both are set, the header's high bits temporarily hold the forwarding pointer (new address). No global forwarding table is needed, but before reclaiming physical memory, all references to the object must be updated . Shenandoah offers rich tunable parameters; tuned well it delivers excellent performance, but tuning effort is significant. It gained generational support in Java 25.
5. Java 26 / 27: Default Collector Changes
Java 26 brings a 5–15% throughput improvement to G1 , enough for G1 to outperform Serial even on small heaps while keeping better latency. Consequently, in Java 27, G1 becomes the default collector for all hardware configurations , replacing the previous rule that used Serial for <2 cores / <2 GB. That rule expires at Java 27. Future directions include: (1) Adaptive Heap — JVM monitors CPU and memory pressure in real time, trading memory for CPU or vice versa, aiming to eliminate -Xms / -Xmx; (2) distinguishing private (request-scoped) vs. shared objects, allowing bulk destruction of request-local objects without standard mark-scan cycles.
6. How to Choose: Summary
General-purpose backend / microservices : Use G1. Best balance of throughput and latency; default from Java 27.
Ultra-low latency (≤1 ms) : Generational ZGC or Shenandoah. Spend 0–20% extra CPU/memory for sub-ms pauses; monitor allocation stalls.
Data processing / batch / queue computing : Parallel GC. Maximize throughput; accept long pauses.
Tiny containers on Java ≤26 : Serial GC. On Java 27+, just use G1.
These scenarios are mutually exclusive — you cannot simultaneously demand lowest pause and highest throughput. Selection boils down to deciding which metric you are willing to pay for.
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.
