Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A service on generational ZGC shows GC pauses below 1 ms, yet p99 latency spikes to 300 ms under peak load. JFR shows ZAllocationStall events. Explain and fix.

An allocation stall means a thread needed memory and had to wait until the concurrent collector freed some. It happens when the allocation rate exceeds what ZGC can reclaim with the available heap headroom and GC threads, and because the waiting thread is a request thread, the wait goes straight into latency while the GC pause metrics stay below 1 ms. The fixes are more heap headroom, more CPU for GC threads, and a lower allocation rate in hot code paths, and we watch allocation rate and GC cycle frequency together to see how close the service runs to the limit.

Why this question is asked

Teams move to ZGC to get rid of GC pauses, check their dashboards, see sub-millisecond pauses and declare victory. Then peak traffic arrives and p99 jumps to 300 ms with no GC pause in sight. The mismatch confuses people because the usual metric for GC trouble, pause time, says everything is fine. Interviewers ask this to see whether we know how a concurrent collector fails. In this answer, we will recap how ZGC keeps up with allocation, see what an allocation stall is, reproduce the arithmetic of running out of headroom, find the evidence in JFR and logs, and go through the fixes.

1A Quick Recap: Concurrent Collection Is a Race

ZGC collects while the application runs. A cycle starts, marks live objects, relocates them, and frees pages. During all that time the application keeps allocating new objects in the free part of the heap. So each cycle is a race: the collector must finish and return memory before the application has used up what was free when the cycle started.

ZGC starts cycles based on its own predictions of allocation rate and cycle duration, aiming to finish before memory runs out. The free memory it has to work with is the headroom: maximum heap minus live data.

2What an Allocation Stall Is

If the race is lost, a thread that tries to allocate finds no free memory. ZGC does not respond with a stop-the-world collection. Instead, that thread blocks until the running cycle frees memory, and then its allocation completes. This wait is an allocation stall.

Notice the consequence. The stall happens to the allocating thread, which in a web service is a request thread. A request that allocates a 2 KB buffer at the wrong moment waits 300 ms. No GC pause is recorded, because the application was never stopped as a whole; other threads that do not allocate keep running. So GC pause metrics stay below 1 ms while request latency spikes.

Step throughA request thread stalls on allocation
Request threadZGC allocatorZGC cycle
cycle running: mark, relocate
allocate 2 KB
no free page left
wait for memory
relocation frees pages, 300 ms later
pages available
allocation done, request continues

7 messages in order. Play them, or step through them one at a time.

No safepoint, no pause line in the log: just one thread waiting.

3Doing the Arithmetic

A small calculation shows how close to the edge a service can run. Suppose the service has a 16 GB heap and 9 GB of live data, so 7 GB of headroom. At normal load it allocates 1.5 GB per second, and a young cycle takes about 2 seconds.

Java
long headroomMb = 16_384 - 9_216; // 7,168 MB free when a cycle starts
long normalRate = 1_500; // MB per second
long cycleSeconds = 2;
long usedNormal = normalRate * cycleSeconds; // 3,000 MB: fits, comfortable
long peakRate = 4_000; // MB per second at peak traffic
long cyclePeak = 2; // or longer: GC threads now compete with busy app threads
long usedPeak = peakRate * cyclePeak; // 8,000 MB > 7,168 MB: threads stall

At peak the application wants more memory during one cycle than the headroom holds. It gets worse, because at peak the CPUs are busy with requests, so GC threads get less CPU and the cycle takes longer, which makes the gap bigger. Generational ZGC helps by reclaiming young garbage quickly, but if the live young data is large or old collections are needed, the race can still be lost.

4Finding the Evidence

JFR records each stall as a jdk.ZAllocationStall event with the thread, the duration and the size requested. With -Xlog:gc, ZGC also prints a line naming the stalled thread and the duration. We can line these up with the latency spikes.

Shell
$ jfr print --events jdk.ZAllocationStall recording.jfr | head
jdk.ZAllocationStall {
startTime = 14:02:11.482
duration = 297 ms // the request waited this long
type = "Small" // the kind of page it needed
size = 2.0 MB // page size requested
eventThread = "http-nio-8080-exec-41" // a request thread, not a GC thread
}

Field names vary a little between JDK versions, so check your version. Next to stalls, look at the allocation rate and the cycle frequency in the ZGC statistics lines of the GC log, which -Xlog:gc+stats prints periodically. If cycles are running back to back at peak, with one starting as soon as the previous ends, the collector is already at its limit before stalls appear. That makes cycle frequency a good early warning to alert on.

5Fixing It

There are three levers, and they combine. The first is headroom: raise -Xmx so the free space during a cycle is larger. In our example, a 20 GB heap gives 11 GB of headroom, enough for 8 GB per cycle with margin. This is usually the quickest fix.

The second is CPU for GC. ZGC adjusts its number of concurrent threads dynamically, but if the container is CPU-throttled at peak, cycles get slower. Giving the container more CPU, or setting -XX:ConcGCThreads higher, lets cycles finish sooner.

The third, and the most lasting, is allocating less. A JFR allocation profile shows which code paths allocate the most. Common culprits are building large strings or JSON trees per request, copying byte arrays between buffers, and boxing in hot loops. Halving the allocation rate halves the memory each cycle must cover, and also lowers GC CPU.

Finally, we keep watching allocation rate and cycle frequency together. A dashboard showing both lets us see, for each service, how much margin is left before the next traffic peak causes stalls.

6Follow-up questions

What does SoftMaxHeapSize do in ZGC?

It sets a soft limit below -Xmx. ZGC tries to keep heap usage under the soft limit by starting cycles earlier, but it is allowed to grow beyond it, up to -Xmx, when that is needed to avoid an allocation stall. For example, -Xmx20g -XX:SoftMaxHeapSize=16g normally keeps the service around 16 GB, while leaving 4 GB of emergency room for peaks. It is a good way to combine a modest footprint with protection against stalls.

Why does the stall not show up as a GC pause?

Because a pause is a safepoint where all application threads stop together. An allocation stall stops only the thread that is allocating, while ZGC keeps working concurrently and other threads keep running. Pause-time metrics measure safepoints, so they miss stalls; we need the stall events or a stall metric.

Would switching back to G1 help?

Not with the latency. G1 would collect in stop-the-world pauses instead, and a service that allocates 4 GB per second on a 16 GB heap with 9 GB live would see frequent, long young and mixed pauses. The fixes for both are the same: more headroom and less allocation.

7Conclusion

In this answer, we saw that a ZGC allocation stall means a thread needed memory and had to wait for the concurrent collector to free some, which happens when allocation outpaces what concurrent cycles can reclaim with the available headroom and GC threads. Stalls block request threads directly and never appear as GC pauses, which is why latency spiked to 300 ms while pauses stayed below 1 ms. We confirmed them with JFR ZAllocationStall events, and fixed them with more headroom, more GC CPU and less allocation in hot paths, while watching allocation rate and cycle frequency to know how much margin remains.

What to say in the interviewAbout 90 seconds aloud

Open with

ZAllocationStall events mean that a thread tried to allocate and there was no free memory, so it had to wait until the concurrent collector freed some. ZGC is in a race: while a cycle runs, the application keeps allocating into the headroom, which is the maximum heap minus the live data.

Then, if they want more

At peak load the allocation rate goes up and GC threads get less CPU, so a cycle can need more memory than the headroom holds. ZGC does not then stop the world; the allocating thread simply blocks.

That thread is usually a request thread, so the request takes 300 milliseconds, but GC pause metrics still show under one millisecond, because only that thread stopped.

I would confirm it by matching the jdk.ZAllocationStall events and the stall lines in the GC log to the latency spikes, and by looking at cycle frequency: cycles running back to back mean the collector is at its limit.

The fixes are more headroom with a larger -Xmx, possibly with SoftMaxHeapSize to keep normal footprint down, more CPU for GC threads, and above all lowering the allocation rate in hot paths, found with a JFR allocation profile. Long term, I would alert on allocation rate and cycle frequency together so we see the margin shrink before users do.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A service on generational ZGC shows GC pauses below 1 ms, yet p99 latency spikes to 300...": https://scrivio-interview-library.netlify.app/java/garbage-collection/9afc338870/