Java / Java 17 Garbage Collection Interview Questions
When should you choose ZGC over G1 GC?
ZGC is the better choice when pause time matters more than raw throughput and the heap is large - it keeps stop-the-world pauses limited to brief root-scanning steps, typically well under 10ms, regardless of whether the heap is 8GB or several terabytes, because marking and relocation happen concurrently via load barriers rather than in the pause itself.
G1's pauses, while tunable, still scale somewhat with the amount of work chosen per collection and can occasionally spike (for example on evacuation failure), which matters for services with strict SLA-driven latency requirements like trading systems, real-time bidding, or interactive APIs with tight P99 targets.
The trade-off is that ZGC's load barriers and colored-pointer bookkeeping add some per-access CPU and memory overhead compared to G1, so for throughput-bound batch workloads where occasional longer pauses are tolerable, G1 (or even Parallel GC) can still be the more efficient overall choice.
More Related questions...