Quick revision
The short answer
First we read the GC logs with phase details to see what dominates the 120 ms young pause, because the fix depends on it. If object copy dominates, too much data survives each young collection, so we shorten object lifetimes or reduce allocation in code; if root or remembered set scanning dominates, we look at those structures. Then we lower MaxGCPauseMillis so G1 shrinks the young generation, never pin it with -Xmn, make sure GC threads get CPU, and test generational ZGC only if G1 still cannot meet the budget.
What to remember
In this answer, we treated the 120 ms pauses as a diagnosis problem. We read the phase breakdown and found that object copy dominated, which means too much data survives each young collection. We reduced allocation and object lifetimes in code, which is often the strongest fix, set MaxGCPauseMillis below the budget and removed -Xmn so G1 could shrink the young generation, and checked that GC threads were not throttled. If G1 still could not meet the budget, generational ZGC is the next candidate, judged on end-to-end latency and cost.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we read the GC logs with phase details to see what dominates the 120 ms young pause, because the fix depends on it. If object copy dominates, too much data survives each young collection, so we shorten object lifetimes or reduce allocation in code; if root or remembered set scanning dominates, we look at those structures. Then we lower MaxGCPauseMillis so G1 shrinks the young generation, never pin it with -Xmn, make sure GC threads get CPU, and test generational ZGC only if G1 still cannot meet the budget.
Why this question is asked
A p99 budget of 50 ms with 120 ms pauses means every request caught in a pause misses its target, and at 1 GB per second of allocation young collections happen often. Interviewers like this scenario because the wrong answer is to start flipping flags. The right answer is a diagnosis loop: measure, find the dominant phase, change one thing, measure again. In this answer, we will recap how a G1 young pause works, read the log to find the cost, go through the fixes in the order we would try them, and see how to confirm the change did not just move the latency somewhere else.
1A Quick Recap of a G1 Young Pause
G1 splits the heap into regions of equal size, a few megabytes each. New objects are allocated in eden regions. When eden fills up, G1 stops the application threads and runs a young collection: it finds the live objects in eden and the survivor regions and copies them, the young evacuation, into new survivor regions or, if they are old enough, into old regions. Then the emptied regions are free again.
The important fact is that a copying collection costs time in proportion to what survives, not to how much garbage there is. Dead objects are never touched. So a young pause is long when a lot of data is still alive at the moment of the collection, or when finding the roots of the young objects is expensive. To find roots, G1 scans thread stacks and the remembered sets, records of which old regions hold references into young regions.
2Reading the Log to Find the Dominant Phase
We enable phase-level logging so each pause is broken down. On current JDKs the unified logging option below prints the time of each phase per pause.
- Log pauses with gc+phases=debugone breakdown per pause
- Object Copy dominates?Yestoo much survives: fix lifetimes and allocationNocheck root scanning
- Root or remembered set scan dominates?Yesreduce old-to-young references, check region sizeNocheck reference processing
- Reference processing dominates?Yescut soft/weak refs and finalizersNocheck CPU and safepoint time
- Change one thing, rerun the same loadcompare p99 and GC CPU
5 steps in order. Play them, or step through them one at a time.
Notice that Object Copy is 96 ms of the 121 ms. That tells us the problem is survival volume: G1 is copying too much live data on each young collection. If instead Scan Heap Roots or Merge Heap Roots dominated, the remembered sets would be the problem, often caused by many references from old objects into young ones. If Reference Processing dominated, the application is creating many soft, weak or phantom references, or finalizable objects. Each case has a different fix, which is why we read before we change.
The exact phase names differ a little between JDK versions, so check your version, but the three families stay the same: copying, root and remembered set scanning, and reference processing.
3Fix 1: Reduce What Survives and What Is Allocated
When copy time dominates, the best fix is in our code. Typical causes are request objects that stay referenced a little too long (for example held in a queue for a batch write), large buffers allocated per request, caches whose entries are created and dropped within seconds, and collections that are built and copied repeatedly. An allocation profile, from JFR or async-profiler in alloc mode, shows which call sites allocate the most bytes.
Reducing the allocation rate is often the most effective change of all. At 1 GB per second, cutting allocation to 600 MB per second makes young collections happen 40 percent less often, and it usually also reduces survivors, because fewer objects are alive at any moment. No flag gives that kind of improvement for free.
4Fix 2: Let G1 Shrink the Young Generation
G1 chooses the young generation size itself, pause by pause, to meet its pause-time goal. The default goal is 200 ms, which is far above our 50 ms budget, so G1 has every reason to use a large eden. We tell it what we need.
The reason this works is that a smaller eden means fewer objects are alive when each collection starts, so less is copied. The cost is more frequent pauses and somewhat more GC CPU. We set the goal below the budget, for example 30 ms for a 50 ms p99, because the request also needs time to do its own work.
But there is a catch: -Xmn, or NewSize and MaxNewSize, fixes the young generation size and switches off exactly this adaptation. Many old tuning guides recommend -Xmn, so it is worth checking the existing flags before anything else.
5Fix 3: Make Sure GC Threads Get CPU
G1 copies objects with several parallel worker threads. If the container has a CPU limit and the process is throttled during a pause, the workers simply stop running, and a 30 ms pause becomes 120 ms. We check the container's CPU throttling metrics during pauses and compare the pause's real time with its user and system time in the log. If real time is much larger than user time divided by the thread count, the threads were waiting for CPU. The fix is a higher CPU limit, fewer competing processes, or a ParallelGCThreads value that matches the CPU actually available.
6When G1 Cannot Meet the Budget
If after code fixes and a lower pause goal G1 still cannot stay under the budget, we test generational ZGC with the same load. ZGC does its copying concurrently, so pauses do not depend on the amount of live data, and pause times well under a millisecond are typical. It needs more CPU and more heap headroom, so we compare the cost too, not only the pause time.
7Follow-up questions
How would you verify a change did not just move latency elsewhere?
We compare the whole request latency distribution, not only GC pause times, under the same replayed or load-tested traffic. A lower pause goal can trade one 120 ms pause for three 35 ms pauses close together, and a request can hit more than one. So we check end-to-end p99 and p99.9, throughput, total GC CPU, and for ZGC the allocation stalls, before and after, over at least an hour of steady load. Only if the end-to-end tail improved and CPU stayed acceptable do we keep the change.
Why not simply give the service a bigger heap?
For young pauses a bigger heap does not help by itself, because the pause time depends on how much survives, not on the total heap size. A bigger heap can even let G1 grow eden further, which makes each pause longer. A bigger heap helps when old generation pressure causes extra mixed or full collections.
8Conclusion
In this answer, we treated the 120 ms pauses as a diagnosis problem. We read the phase breakdown and found that object copy dominated, which means too much data survives each young collection. We reduced allocation and object lifetimes in code, which is often the strongest fix, set MaxGCPauseMillis below the budget and removed -Xmn so G1 could shrink the young generation, and checked that GC threads were not throttled. If G1 still could not meet the budget, generational ZGC is the next candidate, judged on end-to-end latency and cost.
Open with
I would start with the GC logs at phase level, gc plus phases at debug, because a young pause can be long for different reasons. If Object Copy dominates, too much data survives each young collection, since G1 only pays for live objects.
Then, if they want more
Then the best fix is in the code: an allocation profile from JFR or async-profiler shows the hot allocation sites, and cutting the allocation rate or how long objects live reduces both the frequency and the length of pauses.
Next I would make sure the young generation is not pinned with Xmn, because that disables G1's adaptive sizing, and lower MaxGCPauseMillis to around 30 milliseconds so G1 picks a smaller eden. That gives more frequent but shorter pauses. I would also check CPU throttling in the container, because GC worker threads that cannot run stretch the pause.
If roots or remembered sets dominated, or reference processing, I would look at those instead. Every change I would test under the same load and compare end-to-end p99 and p99.9, not just pause times, so I know latency was not simply moved into more frequent pauses.
If G1 still cannot meet 50 milliseconds, I would test generational ZGC and compare CPU and memory cost.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 23
- 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.
- Official documentation
- HotSpot Garbage Collection Tuning Guide, JDK 21
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 G1 service with an 8 GB heap and about 1 GB per second of allocation has a p99 latency...": https://scrivio-interview-library.netlify.app/java/garbage-collection/b056dd7db6/