Video in progress

Strong Middle ⚙️ JVM and memory

"Allocation Stall" shows up in the ZGC log. What does it mean and how do you fix it?

An application thread asked for space for a new object, but the ZGC concurrent cycle hadn't managed to free any yet, so the thread waited for the GC to finish. It is not a formal pause, but for the user it is the same thing as one. Typical causes: a small heap reserve, a very high allocation rate, or not enough CPU for the GC.

📖 Answer

In the log:

[gc] Allocation Stall (http-nio-8080-exec-42) 187.310ms
[gc] GC(911) Major Collection (Allocation Stall) 7800M(95%)->5200M(63%) 2.104s

ZGC pauses are under a millisecond, so the team thinks "GC isn't a problem". But stalls are written on a separate line and may not be counted in the GC pause metric. In JFR there is a jdk.ZAllocationStall event.

Why it happens: a concurrent GC needs a "runway": while it does marking and relocation, the application keeps allocating. If the free space runs out before the cycle ends, the thread waits.

The fixes, from simple to complex:

  1. Make the heap bigger. Concurrent GCs need more reserve than G1. If the live set is 4 GB, an 8 GB heap may be tight for ZGC.
  2. Generational ZGC. -XX:+ZGenerational in JDK 21-22, the default in 23+. Collecting young objects separately cuts stalls sharply. If you are still on non-generational, this is the first step.
  3. CPU. If the pod's CPU limit is low, the GC threads get throttled and the cycle stretches. Raising ConcGCThreads helps only if there is CPU.
  4. -XX:SoftMaxHeapSize. Keep the heap at a "target" smaller than Xmx, and leave the rest as a reserve for peaks.
  5. Reduce allocation: find the places that create the most with JFR or async-profiler's alloc mode.

If stalls are regular, set a separate alert on them: p99 latency spikes often come from exactly this.