Skip to content
Latchkey

Gradle "Java heap space" During Build - Fix Worker Heap in CI

A Gradle compile or code-generation worker ran out of Java heap. Big source sets, annotation processors, or too many parallel workers exceeded the heap available to the JVM doing the work.

What this error means

A task like :compileJava or an annotation-processing step fails with java.lang.OutOfMemoryError: Java heap space. The daemon itself may survive; the worker JVM doing the heavy work is what dies.

gradle output
> Task :app:compileJava FAILED
> java.lang.OutOfMemoryError: Java heap space
FAILURE: Build failed with an exception.

Common causes

Worker/daemon heap too small for the work

The default heap is too low for a large compile or a memory-hungry annotation processor, so the worker exhausts the heap mid-task.

Too many parallel workers

High org.gradle.workers.max runs many JVMs at once; together they exceed the runner RAM and individual workers OOM under contention.

How to fix it

Raise the daemon/worker heap

Set a larger heap in gradle.properties so compile and codegen have room.

gradle.properties
# gradle.properties
org.gradle.jvmargs=-Xmx3g -XX:MaxMetaspaceSize=1g -XX:+HeapDumpOnOutOfMemoryError
org.gradle.workers.max=2

Bound the compiler fork heap

Give the Java compile task its own bounded forked JVM heap.

build.gradle.kts
tasks.withType<JavaCompile> {
    options.isFork = true
    options.forkOptions.memoryMaximumSize = "1500m"
}

How to prevent it

  • Size org.gradle.jvmargs and org.gradle.workers.max to the runner RAM, bound forked compile/test heaps, and use a larger runner for heavy builds.

Frequently asked questions

What causes "Gradle "OutOfMemoryError: Java heap space""?
The default heap is too low for a large compile or a memory-hungry annotation processor, so the worker exhausts the heap mid-task.
How do I fix Gradle "OutOfMemoryError: Java heap space"?
Set a larger heap in gradle.properties so compile and codegen have room.
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