Skip to content
Latchkey

Java "GC overhead limit exceeded" During Build - Fix Heap in CI

The JVM aborted with GC overhead limit exceeded - it spent over 98% of recent time collecting garbage while recovering less than 2% of the heap. The heap is effectively full of live objects; GC is thrashing.

What this error means

A compile, test, or codegen step fails with java.lang.OutOfMemoryError: GC overhead limit exceeded. The build slows dramatically before failing as GC dominates wall-clock time.

java runtime
> Task :app:test FAILED
java.lang.OutOfMemoryError: GC overhead limit exceeded
  at java.base/java.util.HashMap.resize(HashMap.java:702)

Common causes

Heap too small for the live set

The work needs more heap than -Xmx allows. As the heap fills with live objects, GC runs constantly but frees little, tripping the overhead limit.

Excessive allocation or retention

A test or task that retains large structures (big fixtures, accumulating collections) keeps too much live, so GC cannot reclaim enough to make progress.

How to fix it

Raise the heap for the work

Give the JVM enough heap so GC is not thrashing.

Terminal
export MAVEN_OPTS="-Xmx3g -XX:+HeapDumpOnOutOfMemoryError"
mvn -B clean verify
# Gradle: org.gradle.jvmargs=-Xmx3g in gradle.properties

Reduce live-object pressure

  1. Split very large tests or process data in streams/batches instead of loading it all.
  2. Bound forked test JVM heap so each fork has adequate room.
  3. Capture a heap dump on OOM and inspect the dominating retained objects.

How to prevent it

  • Size -Xmx to the live set, bound forked-JVM heaps, and avoid retaining large structures across a build step.

Frequently asked questions

What causes ""GC overhead limit exceeded""?
The work needs more heap than -Xmx allows. As the heap fills with live objects, GC runs constantly but frees little, tripping the overhead limit.
How do I fix "GC overhead limit exceeded"?
Give the JVM enough heap so GC is not thrashing.
Can Latchkey fix this automatically?
Yes. Latchkey runs your GitHub Actions on managed runners that detect this failure, apply the fix, and retry the job automatically - self-healing is on by default.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card