Skip to content
Latchkey

Gradle "OutOfMemoryError: Java heap space" daemon in CI

The Gradle JVM (or a forked compiler/test JVM) exhausted its heap. On CI the heap ceiling from org.gradle.jvmargs is often lower than the build needs, so a large module or test suite overruns it.

What this error means

The build fails with "java.lang.OutOfMemoryError: Java heap space", sometimes with "Expiring Daemon because JVM heap space is exhausted" just before it.

Gradle
> Task :app:compileJava FAILED
java.lang.OutOfMemoryError: Java heap space

* What went wrong:
Execution failed for task ':app:compileJava'.
> java.lang.OutOfMemoryError: Java heap space

Common causes

The -Xmx ceiling is below what the build needs

A default or low -Xmx in org.gradle.jvmargs cannot hold the working set of a large module or a big test run.

A forked JVM has its own low heap

Compilers or tests run in forked JVMs whose heap is set separately and may be too small.

How to fix it

Raise the Gradle JVM heap

  1. Increase -Xmx in org.gradle.jvmargs, staying under the container limit.
  2. Run --no-daemon so the fresh JVM starts with the new ceiling.
  3. Confirm the container has enough memory for that heap plus forks.
gradle.properties
org.gradle.jvmargs=-Xmx4g -XX:MaxMetaspaceSize=512m

Raise heap for forked test/compile JVMs

Set maxHeapSize on the forked JVMs that overrun, separately from the Gradle daemon.

build.gradle.kts
tasks.test {
    maxHeapSize = "2g"
}

How to prevent it

  • Set -Xmx in org.gradle.jvmargs to match the build size.
  • Keep the heap under the runner container memory limit.
  • Set maxHeapSize on forked test and compile JVMs explicitly.

Frequently asked questions

What causes ""OutOfMemoryError: Java heap space""?
A default or low -Xmx in org.gradle.jvmargs cannot hold the working set of a large module or a big test run.
How do I fix "OutOfMemoryError: Java heap space"?
Raise the Gradle JVM heap

Related guides

References

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