Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

CPU is high on a service, but thread dumps and top show that most CPU is used by GC threads, and the heap is nearly full right after each collection. What is happening and what do you do?

The live set, the data still reachable, has grown close to the maximum heap, so each collection frees very little and the next one starts almost at once; the collector runs nearly continuously and uses the CPU. This is GC thrashing, and with Parallel GC it can end in OutOfMemoryError: GC overhead limit exceeded. We confirm it from GC logs, decide from the post-GC trend and a heap histogram whether it is a leak or legitimate growth, and then fix the leak, reduce retained data, or raise the heap and the container limit together.

Why this question is asked

High CPU usually makes us look for hot application code, but when the busy threads are garbage collector threads, the application is not the one working; the JVM is fighting for memory. Recognising this quickly saves hours of looking at flame graphs of our own code. Interviewers ask this question to see whether we understand how heap occupancy drives collector behaviour, and whether we can tell a memory leak from a service that simply needs more memory. In this answer, we will recap how a collector decides when to run, see what thrashing looks like in the logs, find out whether it is a leak, and choose a fix.

1A Quick Recap: Why a Full Heap Makes GC Run Constantly

A garbage collector finds objects that are still reachable from roots, such as thread stacks and static fields, and frees the rest. The time a collection takes depends mostly on the amount of live data it has to trace or copy, while the memory it frees is whatever was garbage. So collections are efficient when the heap holds lots of garbage and little live data.

Now imagine the live set is 3.8 GB in a 4 GB heap. After each collection, only about 200 MB is free. The application fills that in a fraction of a second, so the next collection is needed almost immediately. Each collection still has to deal with the 3.8 GB of live data, which costs a lot of CPU, and frees only a little. The result is that GC threads use most of the CPU, the application threads are paused or slowed for much of the time, and throughput collapses. That is GC thrashing.

2What It Looks Like in the GC Log

The GC log gives us the evidence directly. Each line shows the heap before and after a collection, and the pause time.

Java
# healthy: large drop after each young collection, seconds between collections
[10:00:01] GC(812) Pause Young (Normal) (G1 Evacuation Pause) 3100M->1250M(4096M) 14ms
[10:00:09] GC(813) Pause Young (Normal) (G1 Evacuation Pause) 3120M->1262M(4096M) 15ms
# thrashing: almost nothing freed, collections back to back, full GCs appear
[14:30:00.1] GC(5120) Pause Young (Normal) (G1 Evacuation Pause) 4010M->3890M(4096M) 48ms
[14:30:00.4] GC(5121) Pause Young (Concurrent Start) 4020M->3905M(4096M) 52ms
[14:30:01.2] GC(5123) Pause Full (G1 Compaction Pause) 4090M->3870M(4096M) 2410ms
[14:30:01.9] GC(5124) Pause Young (Normal) (G1 Evacuation Pause) 4030M->3880M(4096M) 50ms
Healthy collection compared with thrashing
SignalHealthyThrashing
Occupancy after GCabout 30% of max heap95% or more of max heap
Time between collectionssecondsa few hundred milliseconds
Full collectionsrare or nonefrequent, seconds each
Share of time in GC1 to 5%50% or more
CPU in GC threadssmallmost of the process

Three signs stand out. The number after the arrow, occupancy after collection, is close to the 4096M maximum, so the live set nearly fills the heap. Collections come a few hundred milliseconds apart instead of seconds. And full collections appear, with pauses in seconds, because the young collections cannot keep up. Adding up pause times over a minute often shows the JVM spending 50 percent or more of its time in GC. The jstat -gcutil command shows the same picture as live percentages if GC logging is not enabled.

3The GC Overhead Limit

The Parallel collector has a built-in safety rule for this situation. If more than 98 percent of the total time is spent in garbage collection and less than 2 percent of the heap is recovered, over several collections in a row, it throws OutOfMemoryError: GC overhead limit exceeded. The idea is that a JVM in this state is doing no useful work, so failing loudly is better than crawling forever.

With G1, the default collector, there is traditionally no such limit, so the service may stay in this state for a long time, slow but alive, until a full collection finally fails and it throws Java heap space. Recent JDK versions may have added a similar limit to G1; check your version. Either way, we should not wait for the error: high CPU in GC threads plus high post-GC occupancy is enough to act.

4Leak or Legitimate Growth?

The collector is the symptom; the question is why the live set is so large. There are two very different answers. A leak means the live set keeps growing over time because something holds objects it no longer needs. Legitimate growth means the service really needs that much data, for example because traffic doubled, a cache was configured larger, or a new feature keeps more state per user.

The post-GC trend tells them apart. We look at heap occupancy after GC over days. A leak shows a steady rise regardless of traffic, including at night, until it hits the ceiling. Legitimate growth tracks traffic or a specific change and then levels off, perhaps just above what the heap allows. A class histogram, jcmd <pid> GC.class_histogram, taken twice a few hours apart, shows which classes take the space and whether they keep growing; a heap dump with its dominator tree shows who holds them.

Step throughDeciding what to do
  1. GC threads busy, post-GC occupancy near max
  2. Look at post-GC trend over days
  3. Rising steadily, even at low traffic?
    YesLeak: find the holder with histograms and a heap dumpNoLegitimate growth: size the memory
  4. Fix the leak or reduce retained databounded caches, smaller batches
  5. Or raise heap and container limit togetherkeep room for native memory

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

5Fixing It

If it is a leak, we fix the code that holds the objects, such as an unbounded cache, listeners never removed or high-cardinality metrics, and confirm the post-GC line goes flat. Raising the heap would only delay the next crisis.

If the growth is legitimate, there are two options. We can reduce the retained data: bound a cache by size, store compact representations instead of full objects, process large results in smaller batches or stream them instead of loading them all. Or we can give the JVM more memory. In a container, raising the heap means raising the container memory limit too, because the JVM needs native memory beyond the heap; if we only raise the heap percentage, the kernel may kill the process instead. A rule of thumb is to size the heap so the steady post-GC occupancy is no more than about half to two thirds of the maximum, which leaves room for allocation bursts and keeps collections efficient.

In the meantime, restarting the affected instances buys time, and for a leak it also resets the clock, but we capture a class histogram or a heap dump first so the restart does not erase the evidence.

6Follow-up questions

Would switching to ZGC or another collector fix this?

No. A different collector changes how and when it collects, but no collector can free memory that is still reachable. ZGC with a nearly full heap will also struggle, and it may stall allocating threads. The fix is in the live set or the heap size, not the collector choice.

Can we disable the GC overhead limit?

With Parallel GC, -XX:-UseGCOverheadLimit turns it off, but that only replaces a clear error with a service that thrashes longer before throwing Java heap space. It hides the symptom without changing the cause, so it is rarely a good idea.

7Conclusion

In this answer, we saw that when GC threads use most of the CPU and the heap is nearly full right after each collection, the live set is close to the maximum heap, so the collector runs almost continuously and frees little. This is GC thrashing; with Parallel GC it can end in GC overhead limit exceeded. We confirmed it from GC logs, used the post-GC trend and class histograms to tell a leak from legitimate growth, and fixed it by removing the leak, reducing retained data, or raising the heap together with the container limit.

What to say in the interviewAbout 100 seconds aloud

Open with

This is GC thrashing. The heap is nearly full right after each collection, which means the live set, the data still reachable, is close to the maximum heap.

Then, if they want more

Each collection has to process all that live data, which costs a lot of CPU, but frees only a little, so the application fills the free space almost immediately and the next collection starts. The GC threads end up using most of the CPU and application throughput collapses.

With Parallel GC, if more than about 98 percent of time is in GC and under 2 percent of the heap is recovered, it throws GC overhead limit exceeded; with G1 it usually limps along until Java heap space.

I confirm it from the GC logs: occupancy after GC at 95 percent of max, collections a few hundred milliseconds apart, and full GCs taking seconds. Then the real question is whether it is a leak or legitimate growth.

I look at post-GC occupancy over several days: a steady rise even at night means a leak, and I compare class histograms hours apart and take a heap dump to find who holds the objects.

If it tracks traffic or a change and then levels off, it is legitimate, and I either reduce retained data, like bounding caches or streaming large results, or raise the heap together with the container limit, aiming for post-GC occupancy around half of max. Changing collector would not help.

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 "CPU is high on a service, but thread dumps and top show that most CPU is used by GC...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/6555ba3927/