Skip to content

Troubleshooting guide

GC overhead limit exceeded in Java: how to fix it

The JVM spent nearly all its time in garbage collection and freed almost nothing, so it gave up. The heap is effectively full of live objects.

3 min read10 sectionsUpdated

On this page

Key points

  • 98 percent of time in GC, under 2 percent recovered: that triggers it.
  • Heap dumps show what is retained.
  • Do not simply disable the check.
  • Set heap as a percentage in containers.

What it means

Applies to the Parallel collector by default. The JVM throws this rather than letting the application crawl forever at near-zero throughput. Other collectors give Java heap space in similar conditions. Either way, live data fills the heap.

Common causes

  • A memory leak: static collections, caches, listeners.
  • Unbounded caches or queues.
  • Loading huge result sets into memory.
  • A heap too small for legitimate data.
  • Many requests each holding large objects.

Diagnose it

Enable GC logging and dump the heap when it fails.

bash
# Recommended flags
java -Xlog:gc*:file=/var/log/gc.log:time \
     -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps \
     -jar app.jar

# Live view of generation occupancy and GC time
jstat -gcutil PID 2000 10

# Top objects by retained count
jcmd PID GC.class_histogram | head -25

# Heap dump on demand (pauses the JVM)
jcmd PID GC.heap_dump /dumps/now.hprof

Fixes

Open the dump in Eclipse MAT, and use the Leak Suspects report. Fix what retains the memory: add eviction to caches, remove listeners, paginate queries. In containers use -XX:MaxRAMPercentage=75 rather than a fixed -Xmx. Then consider G1 or ZGC. Avoid -XX:-UseGCOverheadLimit: it simply removes the warning and lets the application stall until it hits a true out-of-memory error.

Leak or just too small?

Look at heap occupancy after full collections. If it rises across hours until it reaches the limit, you have a leak. If it levels off close to the maximum and rises with traffic, the heap is too small for your workload. A sawtooth with a stable baseline is healthy. In Eclipse MAT, the dominator tree shows which object keeps the most memory alive, and that is usually more telling than the histogram: a million byte[] instances mean little until you see that a single cache owns them.

Catch it automatically

Alert on GC time as a percentage of wall time, and on post-GC heap usage. JVM monitoring charts those next to request latency, which is where GC pressure shows up to users first.

Frequently asked questions

Is this the same as a Java heap space error?

Both mean the heap is effectively full. This one is the JVM giving up early because GC is not productive.

Should I increase -Xmx?

Only after confirming that it is not a leak.

Do heap dumps affect production?

Yes. They pause the JVM, so take them on an instance out of rotation if possible.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.