Fix Java GC Overhead Limit Exceeded Errors and Memory Leaks
Understand why the JVM throws GC overhead limit exceeded, capture a heap dump at the failure and decide between a bigger heap and fixing a memory leak.

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceededThis message is the JVM telling you it is already losing. The garbage collector is running almost all the time and getting almost nothing back, because nearly everything on the heap is still reachable. Giving the program more time will not help, so the collector throws instead of letting the application crawl through a thousand full collections. The question to answer is what is holding the memory, and whether it should be.
This post reproduces the error with a short program, shows why the same program prints a different message under a different collector, and goes through capturing evidence, reading it and choosing between a larger heap and a leak fix. The lab ran on JDK 17.0.1 with a 48 MB heap; the behavior and flags are the same in Java 25.
Reproduce it
The program adds entries to a map and never removes them, which is the most common leak in real code, a cache with no eviction:
import java.util.HashMap;
import java.util.Map;
public class Slow {
public static void main(String[] args) {
Map<Integer, String> cache = new HashMap<>();
try {
for (int i = 0; ; i++) {
cache.put(i, String.valueOf(i));
}
} catch (OutOfMemoryError e) {
int size = cache.size();
cache.clear();
System.out.println(e.getMessage());
System.out.println(size);
}
}
}Run it twice with the same 48 MB heap and a different collector:
java -Xmx48m -XX:+UseParallelGC Slow
java -Xmx48m -XX:+UseG1GC SlowGC overhead limit exceeded
431480
Java heap space
393217Same program, same heap, same bug, two messages. Parallel GC counted the time and the yield, saw both thresholds crossed and threw the overhead error after 431,480 entries. G1 has no such check, so it kept trying until an allocation truly failed, then reported Java heap space at 393,217 entries. If you only know one of these messages, you have only half of the troubleshooting guide. Both are answered by the steps below.
The GC log shows the death spiral. Run with -Xlog:gc and look at the end:
[2.615s][info][gc] GC(43) Pause Full (Ergonomics) 43M->43M(46M) 54.269ms
[2.620s][info][gc] GC(44) Pause Full (Ergonomics) 43M->0M(38M) 2.383msThere were 42 full collections in this short run. Read the first line: before 43M, after 43M, heap 46M. The collector stopped the world for 54 ms and freed nothing. A healthy full GC shows a large drop between the two numbers. A line where before and after are equal is the signature of a live set that fills the heap, and a string of such lines is what trips the overhead limit. The second line, 43M->0M, is the program's own cache.clear() in the catch block releasing the map, so it is an artifact of the demo.
Get evidence at the moment of failure
The failure happens once, probably at night, so make the JVM capture its own state before you need it:
java -Xmx512m \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/var/log/app/ \
-Xlog:gc*:file=/var/log/app/gc.log:time,uptime,level,tags:filecount=5,filesize=20m \
-jar app.jarHeapDumpOnOutOfMemoryError writes a file named java_pid<pid>.hprof into HeapDumpPath when any heap-related OutOfMemoryError is thrown. In the lab it produced a 66 MB file in 31 ms, since the heap was small; a 16 GB heap takes tens of seconds and needs 16 GB of free disk, so check the volume first. In a container, point the path at a mounted volume. A dump written inside the container filesystem disappears with the restart that follows the crash.
If the process is still alive but sick, take the dump by hand and mind the cost, because it pauses the application:
jcmd <pid> GC.heap_dump /var/log/app/manual.hprof
jcmd <pid> GC.class_histogram | head -15The class histogram is a quicker first look than the dump. It counts instances per class without writing a file:
num #instances #bytes class name
1: 431480 13807360 java.lang.String
2: 431480 10355520 java.lang.Integer
3: 431480 ... java.util.HashMap$NodeThree classes with exactly the same count is the tell. They are the key, the value and the map node of one collection, and their count is the number of entries.
Read the dump
Open the .hprof file in Eclipse MAT and run the leak suspects report. The two views that answer the question are:
- Dominator tree. Objects sorted by retained size, the memory that would be freed if that object disappeared. One
HashMapretaining 90 percent of the heap is the answer, and its path from a GC root tells you which static field or thread owns it. - Path to GC roots. For any suspect, choose "exclude weak references" and see what keeps it alive: a static map, a
ThreadLocal, a listener list, a cache.
The walkthrough, including OQL queries, is in capturing and analyzing a heap dump with MAT. The usual suspects in production code are an unbounded in-memory cache, a collection that accumulates per-request data in a singleton, a result set read whole (findAll() on a big table), a listener that is registered and never removed, and a ThreadLocal on a pool thread.
Is it a leak or a real size?
The two need opposite fixes, and the quickest separator is the heap after a full collection, over time. Pull it from the log:
grep "Pause Full" gc.log | sed -E 's/.*GC\(([0-9]+)\).* ([0-9]+)M->([0-9]+)M.*/\1 \3/' | tail -10If the number after each full GC is flat, the live set is stable and the heap is just too small for the working set: raise -Xmx. If the number steps upward with every collection, something is accumulating, and a bigger heap only moves the failure later. A leak test on a staging copy makes it plain: replay a fixed workload for an hour, record the after-GC figure every few minutes and plot it. A leak is a staircase.
For sizing the fix, work from the live set and not from guesses. A common rule is to give the heap two to three times the live set after a full collection, so the collector has room to work. A live set of 400 MB suggests a heap of 800 MB to 1.2 GB. In a container, check the limit has room for the rest of the process, see heap size in containers.
Switching collectors changes the symptom
Do not read too much into the wording. -XX:-UseGCOverheadLimit disables the check on Parallel GC, and the program in the lab then ends with Java heap space, as G1 does. The leak remains, the message changes, and the cost is a slow crawl before the crash. If a post-mortem says "we fixed the overhead error by turning off the limit", nothing was fixed. The other direction is useful, though: if a service logs Java heap space on G1 and you cannot read a heap dump, reproducing the same load on Parallel GC can produce the clearer overhead message. The collector choice itself is a separate question, covered in G1, ZGC and Shenandoah.
Quick triage
| Observation | Likely cause | Next step |
|---|---|---|
| After-GC heap rises with every full GC | Leak | Dump, dominator tree, find the owner |
After-GC heap flat and close to -Xmx | Heap too small | Raise -Xmx, re-measure |
One huge object (a byte[], a list) | A single oversized read | Stream or paginate instead of loading it all |
| Error right after a deploy | New code path accumulating state | Diff the release, look for new static collections |
| Error only under load | Per-request data retained for too long | Check caches, session size and thread locals |
| Metaspace in the message instead | Class metadata, not the heap | See Metaspace OutOfMemoryError |
Where this does not apply
If the process is killed with no Java message at all, the container or the kernel stopped it, and the heap flags are not the cause. The log to read is the orchestrator's, see OOMKilled. If the heap looks fine and pauses are long, you have a tuning question instead, see reading GC logs, and the commands for live inspection are in using jcmd, jstack and jmap safely. For the regions of memory the JVM manages besides the heap, read JVM memory areas. The primary reference is the JDK troubleshooting guide.


