Video in progress
List the main garbage collectors in HotSpot and say when you would pick each.
Flag / In short / When — Serial: -XX:+UseSerialGC / one thread, all work in the pause / small heap, 1 CPU, CLI utilities; Parallel: -XX:+UseParallelGC / many threads, but in the pause / batch, ETL: overall speed matters, not the pause; G1: -XX:+UseG1GC / regions, a pause goal / most web services; ZGC: -XX:+UseZGC /…
📖 Answer
| GC | Flag | In short | When |
|---|---|---|---|
| Serial | -XX:+UseSerialGC |
one thread, all work in the pause | small heap, 1 CPU, CLI utilities |
| Parallel | -XX:+UseParallelGC |
many threads, but in the pause | batch, ETL: overall speed matters, not the pause |
| G1 | -XX:+UseG1GC |
regions, a pause goal | most web services |
| ZGC | -XX:+UseZGC |
almost fully concurrent | low latency, large heap (tens of GB) |
| Shenandoah | -XX:+UseShenandoahGC |
concurrent compaction | a goal similar to ZGC; not in every distribution |
There is a trade-off between throughput and latency. Parallel GC uses the CPU most efficiently, but its pauses can be long. ZGC shortens the pause, at the cost of somewhat more CPU and memory.
A note on versions: ZGC has been production-ready since Java 15. In Java 21 the generational mode is turned on with -XX:+ZGenerational, and from Java 23 it is the default mode.
In practice I usually start with G1, and only switch to another and measure if the GC log or latency metrics show a problem.
Sign up to mark studied questions and save your progress.