Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

A service with a 31 GB heap on Parallel GC has full GC pauses of 12 seconds every few hours because the old generation fills with long-lived cache entries. The team wants to move to ZGC with a 64 GB heap. How do you evaluate this?

The 12-second pauses come from Parallel GC's stop-the-world mark-compact of a large live set, and ZGC would remove them by marking and relocating concurrently. But going from 31 GB to 64 GB also loses compressed oops, so references grow from 4 to 8 bytes, and ZGC needs CPU and heap headroom to keep up with allocation. We measure live set, allocation rate and CPU budget, question whether a huge on-heap cache is the right design at all, and decide with a canary on production traffic comparing p99, throughput and cost.

Why this question is asked

A 12-second pause every few hours is a reliability problem: health checks fail, load balancers mark the instance dead, and clients retry. The team's proposal, a different collector with twice the memory, may well fix it, but it changes the service's cost and memory model at the same time. Interviewers use this scenario to see whether we can evaluate a plausible fix instead of accepting or rejecting it on instinct. In this answer, we will explain why the pauses happen, what ZGC changes, the hidden effect of crossing 32 GB, what ZGC needs to work well, the design alternative, and how to run the evaluation.

1Why Parallel GC Pauses for 12 Seconds

Parallel GC collects the old generation with a stop-the-world mark-compact: all application threads stop, the collector marks every live object, then slides them together and updates every reference. All of this is proportional to the live data in the old generation. A cache of long-lived entries is precisely live data, so as the cache fills toward 20 GB or more, each full collection has more to mark and move, and 12 seconds is a realistic result even with many GC threads.

Notice that the cache entries are not garbage. The collector spends those 12 seconds rediscovering and moving objects that are still in use. That is the core of the problem, and it is worth keeping in mind when we reach the design options.

2What ZGC Changes

ZGC marks and relocates objects concurrently, while the application runs, using load barriers on coloured pointers. Its pauses are limited to short synchronisation points, typically well under a millisecond, and they do not grow with the heap or the live set. So the 12-second pause would disappear. Generational ZGC, the default ZGC mode since JDK 23 and the only mode since JDK 24, also collects young objects separately, which keeps the cost of a large stable cache manageable because the cache sits in the old generation and is not re-marked on every young cycle.

3The Hidden Cost of Crossing 32 GB

Below about 32 GB of heap, HotSpot uses compressed oops: object references are stored in 4 bytes and scaled by the 8-byte object alignment, which can address up to 32 GB. That is probably why the heap is 31 GB today. Above that limit, references take 8 bytes. ZGC does not use compressed oops at all, at any heap size, because it needs the extra bits for its coloured pointers.

Shell
java -XX:+UseParallelGC -Xmx31g -XX:+PrintFlagsFinal -version | grep UseCompressedOops
# bool UseCompressedOops = true
java -XX:+UseParallelGC -Xmx33g -XX:+PrintFlagsFinal -version | grep UseCompressedOops
# bool UseCompressedOops = false
// A cache entry with an object header and 6 reference fields
// compressed oops: ~12-byte header + 6 x 4 = ~36 bytes (40 with alignment)
// uncompressed: ~12-byte header + 6 x 8 = ~60 bytes (64 with alignment)

So the same cache can take noticeably more memory under ZGC, often 20 to 40 percent more for reference-heavy structures such as maps of small objects. Part of the extra 33 GB the team asks for will simply be absorbed by bigger references, and the effective headroom is less than it looks. We measure the live set under ZGC rather than assuming it stays at today's size.

4What ZGC Needs to Keep Up

ZGC is a concurrent collector, so it must finish each cycle before the application runs out of free memory. That depends on two numbers: the allocation rate and the headroom between the live set and the maximum heap. If the service allocates 1 GB per second and the live set is 30 GB in a 64 GB heap, there are 34 GB of headroom, so ZGC has about 30 seconds to complete a cycle. If it falls behind, threads hit allocation stalls, which are pauses of their own, recorded in the GC log.

It also needs CPU. ZGC's concurrent threads compete with the application for cores. On a pod with 8 cores, collector threads might use one or two of them during cycles. If the pod is already near its CPU limit, request latency rises from CPU contention instead of GC pauses. So the evaluation must include CPU headroom and, where the platform throttles, CPU limits.

What to measure before and during the test
MeasureWhy it mattersWhere to get it
Live set after collectionsets the heap and headroomGC log, heap after full or mark
Allocation ratehow fast ZGC must workjvm.gc.memory.allocated, JFR
CPU headroomZGC threads share corescontainer CPU and throttling metrics
Allocation stallsZGC fell behindGC log 'Allocation Stall' lines
p99 latency and throughputthe real outcomeservice metrics on the canary

5Questioning the Design

Before buying 64 GB per instance, we ask whether a 20 to 30 GB on-heap cache belongs in the JVM. The options include an off-heap cache, which stores entries in native memory and keeps them out of the collector's work entirely; a separate cache service such as Redis or a distributed cache, shared by all instances and surviving restarts; or a bounded on-heap cache with a size limit and eviction, for example Caffeine with maximumWeight, if most of the value comes from a small hot set. Each option has costs: serialisation for off-heap, network latency for a remote cache, lower hit rate for a smaller cache. But any of them may be cheaper than doubling memory on every instance, and they also shorten startup, because a huge on-heap cache must be warmed again after every deploy.

6Running the Evaluation

We run a canary: one or a few instances on ZGC with the proposed heap, receiving a share of real production traffic, next to the current Parallel GC instances. We compare p99 and p99.9 latency, throughput per instance, CPU use, memory use including the larger references, and allocation stalls over at least a day, so that several cache fill cycles happen. If ZGC removes the long pauses within the CPU budget and the cost per request is acceptable, we roll it out gradually. If the memory cost is too high, the design options become the better path, and we may use ZGC with a smaller, bounded cache.

Step throughEvaluating the move
  1. Measure live set, allocation rate, CPU headroomon today's Parallel GC instances
  2. Estimate ZGC heap with 8-byte referenceslive set grows for reference-heavy data
  3. Canary ZGC on real trafficat least a day, several cache cycles
  4. Long pauses gone, no stalls, cost acceptable?
    Yesroll out graduallyNoconsider off-heap, remote or bounded cache

4 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Could G1 solve the problem instead of ZGC?

Possibly. G1 marks the old generation concurrently and reclaims it through mixed collections, so in normal operation it avoids full collections, with pause times usually in the tens of milliseconds. It keeps compressed oops at 31 GB. If the latency budget tolerates G1 pauses, it is a cheaper first test. The risk is a full GC if mixed collections cannot keep up with promotion of cache entries.

Does a 33 GB heap make sense?

Usually not. Just above the compressed oops limit, references double in size, so a 33 GB heap may hold less data than a 31 GB heap. If we go over the limit, we go well over it, for example to 48 GB or more, to make the change worthwhile. With ZGC the limit does not matter, since it never uses compressed oops.

Why does a large on-heap cache hurt even with ZGC?

ZGC still has to mark the cache during old generation cycles, which costs CPU, and a large live set needs proportionally more heap for headroom. The cache also has to be rebuilt after each restart, so deploys and crashes cause a period of cold cache and higher load on the backing store.

8Conclusion

In this answer, we saw that Parallel GC's 12-second pauses come from a stop-the-world mark-compact over a large live cache, and that ZGC's concurrent marking and relocation would remove them. We also saw the hidden costs: crossing 32 GB and using ZGC both mean 8-byte references, so the live set grows, and ZGC needs allocation headroom and CPU to keep up. We questioned the on-heap cache itself and decided with a canary on production traffic, comparing latency, throughput and cost.

What to say in the interviewAbout 80 seconds aloud

Open with

The 12-second pauses are Parallel GC's full collection, a stop-the-world mark-compact whose pause grows with live data, and a large cache is all live data. ZGC would fix the pauses, because it marks and relocates concurrently and its pauses stay under a millisecond regardless of heap size.

Then, if they want more

But I would evaluate the proposal carefully. First, 31 GB is probably chosen to keep compressed oops; above about 32 GB references become 8 bytes, and ZGC never uses compressed oops anyway, so the cache will take more memory, maybe 20 to 40 percent more for reference-heavy data.

Second, ZGC needs headroom to keep up with the allocation rate, otherwise we get allocation stalls, and its concurrent threads need CPU, so I would measure live set, allocation rate and CPU headroom first.

Third, I would question the design: a 20 to 30 GB on-heap cache might be better off-heap, in a separate cache service, or bounded with eviction, which also helps startup. G1 is also worth testing at 31 GB.

Finally I would run a canary with real traffic for at least a day and compare p99 and p99.9 latency, throughput, allocation stalls and memory cost per request before deciding.

About this answer

Last updated
12 November 2026
Versions it names
Java 23, Java 24
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 with a 31 GB heap on Parallel GC has full GC pauses of 12 seconds every few...": https://scrivio-interview-library.netlify.app/java/garbage-collection/7402e20b9f/