Java / Java 17 Garbage Collection Interview Questions
What is an evacuation failure in G1 GC and how do you handle it?
An evacuation failure (sometimes logged as "to-space exhausted") happens when G1 starts copying live objects out of the regions it's collecting but runs out of free regions to copy them into partway through the pause.
Because objects can't simply be left half-copied, G1 has to fall back to a safety mechanism: it marks the objects that couldn't be evacuated in place within their original regions and, if the situation is severe enough, this cascades into triggering a full, single-threaded, stop-the-world compaction - by far the most expensive collection G1 can run, and one that defeats the whole point of choosing a low-pause collector in the first place.
The usual causes are a heap that's simply too small for the live-data working set, a sudden burst in allocation rate that outpaces the concurrent cycle, or too little reserved headroom. Mitigations include increasing overall heap size, raising -XX:G1ReservePercent (the fraction of heap kept in reserve for evacuation, default 10%) to give more breathing room, and lowering or adaptively tuning -XX:InitiatingHeapOccupancyPercent so the concurrent marking cycle - and therefore reclamation - starts earlier, before the heap gets that tight.
More Related questions...