OutOfMemoryError: GC Overhead Limit Exceeded

java.lang.OutOfMemoryError: GC overhead limit exceeded

Quick Answer

The garbage collector is spending more than 98% of time collecting with less than 2% of heap recovered. You have a memory leak or insufficient heap.

Why This Happens

This error means the JVM is spending almost all its time doing garbage collection but recovering almost no memory. This typically indicates that the heap is nearly full with live objects that cannot be collected, usually due to a memory leak or processing a dataset too large for the allocated heap.

The Problem

Map<String, String> cache = new HashMap<>();
for (int i = 0; i < 10_000_000; i++) {
    cache.put("key" + i, "value" + i); // Unbounded cache growth
}

The Fix

// Use a bounded cache with LRU eviction
Map<String, String> cache = new LinkedHashMap<>(1000, 0.75f, true) {
    @Override
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > 1000;
    }
};

Step-by-Step Fix

  1. 1

    Identify memory-heavy objects

    Take a heap dump and analyze it with Eclipse MAT or VisualVM to find the largest object groups.

  2. 2

    Find the retention path

    Trace the GC roots to find why large objects cannot be collected.

  3. 3

    Fix the leak or bound the data

    Use bounded caches, process data in streams, close resources properly, or increase heap size with -Xmx.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free