Video in progress
For a service with a 24 GB heap and a p99 latency that must not exceed 50 ms, how do you choose between G1 and generational ZGC?
First I measure G1's actual pause distribution today and what share of the latency budget the pauses take. If G1's pauses fit the budget, I keep it and tune it; if they don't, I try ZGC as a canary on real traffic and compare p99, CPU and memory use.
📖 Answer
Choosing a GC is a trade-off, there is no "best GC". To decide I need numbers.
1. The current state. From the G1 log (-Xlog:gc*:file=gc.log:time,uptime,level,tags) I get the pause distribution. The important question: how much of the p99 request latency is GC? If GC pauses are 30-80 ms and they collide with p99, the GC is to blame. If pauses are 10 ms and p99 is still 120 ms, the problem is elsewhere (DB, a lock, an external service), and changing the GC won't help.
2. Tuning G1. On a 24 GB heap G1 can reach 50 ms in many cases: lower MaxGCPauseMillis to 50-100, get rid of humongous allocations, reduce the allocation rate, give the live set enough reserve. Mixed GC pauses are often the longest.
3. When ZGC. The live set is big and variable, G1's evacuation pauses grow with the live objects, and the latency requirement is around 10 ms or tighter. Then generational ZGC (JDK 21+) is a strong candidate.
What you pay for ZGC:
- CPU may go up by 5-15% because of the load barrier (depends on the load);
- no compressed oops, so heap usage is higher in some applications;
- if there is little free space, an allocation stall: not a pause, but just as bad for latency.
4. Testing. One canary pod with ZGC, the rest on G1. On the same traffic for at least a few days: p50/p99/p99.9, CPU, RSS, GC thread time, allocation stalls. Then decide.
And of course, take the CPU limit into account. ZGC does its concurrent work in the background, so if the pod is limited to 2 vCPUs, it competes with the application for CPU and the result may be worse than expected.
Sign up to mark studied questions and save your progress.