The Real Names#
Choose with a flag at start-up. Find out which one you're running: java -XX:+PrintCommandLineFlags -version printed -XX:+UseG1GC on this Mac, along with the
automatic heap sizes (Ch 10).
| Collector | Flag | Design | Pauses | Best for |
|---|---|---|---|---|
| Serial | -XX:+UseSerialGC | one GC thread; STW generational | long | tiny heaps, single-CPU containers, small tools |
| Parallel | -XX:+UseParallelGC | many GC threads; STW generational | medium–long | throughput: batch jobs, data processing |
| G1 (Garbage-First) | -XX:+UseG1GC — default since Java 9 | heap split into equal regions (1–32 MB); young + old regions; concurrent marking; collects the regions with the most garbage first within a pause-time target | usually tens of ms (target -XX:MaxGCPauseMillis=200 by default) | general purpose — most services |
| ZGC | -XX:+UseZGC | almost everything concurrent, including compaction (using coloured pointers + load barriers); generational from Java 21 (-XX:+ZGenerational), default generational from Java 23 | under 1 ms, independent of heap size (up to terabytes) | low latency, big heaps |
| Shenandoah | -XX:+UseShenandoahGC | concurrent compaction (from Red Hat) | very low | low latency (in OpenJDK builds) |
| Epsilon | -XX:+UseEpsilonGC | never collects | none | performance testing only |
Under the Hood — G1 in one picture#
┌──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┐ │E │E │S │O │O │ │E │H │H │O │ │E │ E = Eden region, S = survivor, O = old, ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤ H = humongous (a big object spanning regions, Ch 3), │O │ │E │O │E │O │ │O │E │ │S │O │ blank = free └──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┘ young GC: evacuate (copy) live objects out of all E and S regions mixed GC: also evacuate the old regions with the MOST garbage — "garbage first" — as many as fit in the pause target
ZGC with too small a heap — the same 3-million-note program on ZGC with a 256 MB heap:
[0.004s][info][gc] Using The Z Garbage Collector
[0.144s][info][gc] GC(0) Garbage Collection (Allocation Stall) 256M(100%)->8M(3%)An allocation stall means the application allocated faster than the concurrent collector could free memory in a heap this small, so allocating threads had to wait. Concurrent collectors need headroom: give ZGC a heap comfortably larger than the live data.
How to choose:
Start with the default, G1. Most services never need anything else.
If p99 latency matters and GC pauses are visible in the logs → try ZGC (Generational ZGC on Java 21+).
If it's a batch job where total time matters and pauses don't → Parallel.
Tiny container with one CPU → the JVM may pick Serial automatically; that's fine.
Before switching collectors, reduce allocation (fewer temporary objects, primitive arrays instead of boxed collections) — the cheapest garbage is garbage never created.
Check yourself
4 questionsWhich collector is the default since Java 9, and what's its core idea?
Show answerHide answer
G1 — the heap is divided into regions and it collects the regions with the most garbage first, within a pause-time target.
What does "garbage first" mean in G1?
Show answerHide answer
In mixed collections it chooses the old regions containing the most garbage, getting the most memory back per unit of pause time.
What's ZGC's main advantage, and what does an "allocation stall" mean?
Show answerHide answer
Sub-millisecond pauses regardless of heap size; an allocation stall means allocation outpaced concurrent collection, so threads waited — usually the heap is too small.
Which collector for: a nightly batch job; a latency-sensitive API with a 64 GB heap; a general web service?
Show answerHide answer
Parallel; ZGC; G1 (the default).