Quick revision
The short answer
Probably not. A leak means the live data keeps growing, so the heap left after each full GC would rise from cycle to cycle; here every full GC brings the heap back to the same 1 GB, which means the extra 2.5 GB was garbage all along. The real problem is that G1's concurrent marking and mixed collections are not reclaiming old regions fast enough, so we check when marking starts, whether mixed collections run, and how objects get promoted, and then start marking earlier, give G1 more room, or shorten object lifetimes.
What to remember
In this answer, we saw that a heap that returns to the same floor after every full GC is not leaking: its live set is stable, and the climb between full GCs is garbage that G1 failed to reclaim in time. We read the GC log to check whether concurrent marking starts, completes and is followed by productive mixed collections, and used the age table to spot premature promotion. The fixes are to start marking earlier, give G1 more heap or concurrent threads, and above all reduce how long objects live.
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.
Probably not. A leak means the live data keeps growing, so the heap left after each full GC would rise from cycle to cycle; here every full GC brings the heap back to the same 1 GB, which means the extra 2.5 GB was garbage all along. The real problem is that G1's concurrent marking and mixed collections are not reclaiming old regions fast enough, so we check when marking starts, whether mixed collections run, and how objects get promoted, and then start marking earlier, give G1 more room, or shorten object lifetimes.
Why this question is asked
A rising line in a heap graph makes people nervous, and the first word that comes to mind is leak. In practice, a sawtooth like this one is far more often a collector that is falling behind than code that is holding on to memory. Interviewers ask this because telling the two apart is a basic skill for anyone who runs Java services: the fixes are completely different,. In this answer, we will recap how G1 is supposed to clean the old generation, look at how a leak differs from this pattern, read the GC log to find out which step is failing, and then go through the changes that fix it.
1A Quick Recap of How G1 Cleans the Old Generation
G1, the default garbage collector in HotSpot, divides the heap into many equal regions, typically a few megabytes each. Some regions are young (where new objects are allocated) and some are old (where objects go once they survive several young collections). A young GC is a short pause that copies the surviving young objects into survivor or old regions and frees the rest.
Young GCs never look inside old regions, so old regions can only be cleaned in two ways. The normal way is a concurrent marking cycle: while the application keeps running, G1 traces every live object to learn how much live data each old region holds. After marking, G1 runs a series of mixed collections, which are young GCs that also evacuate the old regions with the most garbage. The emergency way is a full GC, a long stop-the-world pause that compacts the whole heap. A healthy G1 heap should almost never need the emergency way.
G1 starts marking when old occupancy crosses the initiating heap occupancy threshold, called IHOP. By default this threshold is adaptive: G1 measures how long marking takes and how fast the old generation fills, and tries to start marking just early enough to finish before the heap runs out. The starting value is 45 percent of the heap.
2Leak or Not: Look at the Floor, Not the Peak
The single most useful number here is the heap occupancy right after each full GC, because a full GC frees everything that is not reachable. Whatever remains is the live set. If the live set is the same 1 GB after every cycle, the application is not accumulating anything: it creates objects that live for a while and then die. The 2.5 GB that piles up between full GCs is dead data that G1 simply has not reclaimed yet.
A real leak looks different. The floor itself climbs: 1.0 GB after the first full GC, 1.4 GB after the second, 1.9 GB after the third. The full GCs come closer and closer together because each one frees less, and eventually the JVM spends most of its time collecting and throws OutOfMemoryError: Java heap space.
| Cycle | Heap after full GC here | Heap after full GC with a leak |
|---|---|---|
| 1st full GC | 1.0 GB | 1.0 GB |
| 2nd full GC | 1.0 GB | 1.4 GB |
| 3rd full GC | 1.0 GB | 1.9 GB |
| 4th full GC | 1.0 GB | 2.6 GB, cycles getting shorter |
| Outcome | repeats forever, with long pauses | OutOfMemoryError: Java heap space |
3Why Old Regions Fill With Garbage
If the heap returns to 1 GB, the old generation is filling with what we call medium-lived garbage: objects that survive long enough to be promoted into old regions, but die minutes or hours later. Typical examples are session data and cached results with a long expiry.
Once they are in old regions, only marking plus mixed collections can free them. So one of three things is going wrong. Marking may not start at all, or start so late that the heap fills before it finishes. Marking may finish, but the mixed collections that follow may reclaim too little, because G1 by default skips regions that are more than 85 percent live and stops once the remaining garbage is below 5 percent of the heap. Or objects may be promoted too early, for example because the survivor space overflows, so data that should have died young lands in old regions and inflates the problem.
4Reading the GC Log to Find the Failing Step
We first turn on detailed logging. The tags gc* give the overall picture, and gc+age=trace prints how many bytes of each age sit in the survivor space after every young GC.
- Does the floor after full GC rise?Compare heap after each Pause FullYesTreat it as a leak: take heap dumpsNoCollector is falling behind
- Concurrent Start before the full GC?Search for Concurrent Start linesYesCheck whether marking finishesNoStart marking earlier
- Concurrent Mark Abort?Heap filled during markingYesStart earlier or add ConcGCThreadsNoCheck the Mixed pauses
- Do Mixed pauses free much?Compare before and after sizesYesCheck promotion with gc+ageNoRegions too live: tune mixed or lifetimes
4 steps in order. Play them, or step through them one at a time.
Each question narrows down which step of the old-generation cleanup is failing.
Notice what to look for. If there is no Concurrent Start line before the full GC, marking never began, and the adaptive IHOP started too late. If we see Concurrent Mark Abort, marking began but the heap filled first. If marking completes but the Mixed pauses free only a little, the regions are too live to be chosen. And if the age table shows survivors overflowing, with most bytes promoted at age 1 or 2 instead of reaching the tenuring threshold, objects are being promoted prematurely.
5What We Change
First, we help G1 start sooner. We can give the adaptive IHOP a bigger safety margin with -XX:G1ReservePercent (default 10), or switch adaptation off with -XX:-G1UseAdaptiveIHOP and set a fixed, lower -XX:InitiatingHeapOccupancyPercent such as 30. If marking aborts because it is too slow, more concurrent threads with -XX:ConcGCThreads let it finish sooner, at the cost of some CPU while it runs.
Second, we give G1 more room. A bigger heap makes the gap between marking start and heap full larger, which is often enough on its own when the live set is only 1 GB of 4 GB.
Third, and best, we shorten object lifetimes in the code. For example, a cache that keeps results for an hour might keep them for ten minutes. Objects that die young are almost free for G1, while objects that die old are expensive. After each change we compare the log again: the goal is a run of several days with Mixed pauses and no Pause Full at all.
6Follow-up questions
How would the same pattern look in a heap that truly leaks?
The sawtooth would still be there, but its floor would climb. Each full GC would leave more live data behind than the last one, the time between full GCs would shrink, and young GCs would become more frequent as free space disappears. In the end the JVM throws OutOfMemoryError: Java heap space or spends most of its time in GC. To confirm, we take two heap dumps hours apart, ideally right after a full GC, and compare which classes grew. A leak shows one dominator, such as a map or a list, whose retained size keeps increasing.
Is a full GC in G1 always a sign of misconfiguration?
For a long-running service with steady traffic, yes, it is a sign that something should change. A full GC is G1's fallback when marking and mixed collections fail, so one every six hours is a repeating failure that costs a multi-second pause each time.
7Conclusion
In this answer, we saw that a heap that returns to the same floor after every full GC is not leaking: its live set is stable, and the climb between full GCs is garbage that G1 failed to reclaim in time. We read the GC log to check whether concurrent marking starts, completes and is followed by productive mixed collections, and used the age table to spot premature promotion. The fixes are to start marking earlier, give G1 more heap or concurrent threads, and above all reduce how long objects live.
Open with
I would say this is very likely not a leak. The number that decides it is the heap after each full GC, because that is the live set.
Then, if they want more
Here it goes back to 1 GB every time, so live data is stable; a leak would show that floor rising from cycle to cycle, with full GCs getting closer together. What I see instead is old regions filling with medium-lived garbage that G1 does not reclaim before the heap is full.
I would turn on Xlog gc star and gc plus age at trace level and check three things: whether a Concurrent Start appears before the full GC, whether marking completes or aborts, and whether the Mixed pauses actually free old regions. The age table also tells me whether survivor overflow is promoting objects too early.
Then I would fix the step that fails. If marking starts too late, I raise G1ReservePercent or set a fixed, lower InitiatingHeapOccupancyPercent. If marking is too slow, I add ConcGCThreads. If the container allows it, more heap widens the window.
And I would look in the code for long-held caches or session data, because shortening object lifetimes is the change that helps most. Success is days of running with mixed collections and no full GC.
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 runs with -Xmx4g on G1. GC logs show old generation occupancy after each young...": https://scrivio-interview-library.netlify.app/java/memory-layout/fcbbc7c602/