Gradle "Java heap space" During Build - Fix Worker Heap in CI
By Daniel Zoghalchali·Latchkey
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.
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.