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.
> Task :app:compileJava FAILED
java.lang.OutOfMemoryError: Java heap space
* What went wrong:
Execution failed for task ':app:compileJava'.
> java.lang.OutOfMemoryError: Java heap spaceCommon 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
- Increase
-Xmxinorg.gradle.jvmargs, staying under the container limit. - Run
--no-daemonso the fresh JVM starts with the new ceiling. - Confirm the container has enough memory for that heap plus forks.
org.gradle.jvmargs=-Xmx4g -XX:MaxMetaspaceSize=512mRaise heap for forked test/compile JVMs
Set maxHeapSize on the forked JVMs that overrun, separately from the Gradle daemon.
tasks.test {
maxHeapSize = "2g"
}How to prevent it
- Set
-Xmxinorg.gradle.jvmargsto match the build size. - Keep the heap under the runner container memory limit.
- Set
maxHeapSizeon forked test and compile JVMs explicitly.