Gradle Daemon Out of Memory Fix
In this tutorial, you'll learn about Gradle Daemon Out of Memory Fix. We cover key concepts, practical examples, and best practices.
Your Gradle build fails with OutOfMemoryError: Java heap space, PermGen space, or GC overhead limit exceeded — the Gradle daemon ran out of memory while processing large projects, multi-module builds, or heavy annotation processing.
Step-by-Step Fix
1. Increase JVM heap for the Gradle daemon
# gradle.properties (project-level or ~/.gradle/gradle.properties)
# Wrong: default 512 MB is too small for large projects
org.gradle.jvmargs=-Xmx512m
# Right: increase to 2 GB or 4 GB for large projects
org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError
# Very large projects may need 4 GB:
# org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=1024m
2. Configure parallel builds
# gradle.properties
# Enable parallel builds (uses multiple CPU cores)
org.gradle.parallel=true
# Enable build caching
org.gradle.caching=true
# Configure number of parallel threads (default is CPU cores)
org.gradle.workers.max=4
3. Reduce daemon idle timeout
# gradle.properties
# Wrong: daemon stays in memory for 3 hours (default)
# Right: reduce daemon idle timeout to 10 minutes
org.gradle.daemon.idletimeout=600000 # 10 minutes in milliseconds
4. Kill and restart the daemon
# Check running daemons
./gradlew --status
# Expected output:
# No Gradle daemons are running.
# PID STATUS INFO
# 12345 BUSY 4.7.8
# Stop all daemons
./gradlew --stop
# Or kill the daemon process directly
kill -9 <pid>
# Verify no daemons are running
./gradlew --status
5. Disable the daemon temporarily
# Run without the daemon (gets a fresh JVM each time)
./gradlew build --no-daemon
# This avoids corrupted daemon state issues
# but is slower because the JVM starts fresh each time
6. Optimize build script compilation
// build.gradle
// Wrong: heavy annotation processing in every build
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
}
// Right: enable incremental annotation processing
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments += [
"room.schemaLocation": "$projectDir/schemas".toString(),
"room.incremental": "true]
]
}
}
}
}
// For Kotlin, enable incremental compilation
kotlinOptions {
freeCompilerArgs += ["-Xjvm-default=all"]
incremental = true
}
7. Reduce memory per worker
# gradle.properties
# If parallel builds cause OOM, reduce parallelism
org.gradle.parallel=true
org.gradle.workers.max=2 # Reduce from default (CPU count)
# Or disable parallel builds entirely
# org.gradle.parallel=false
8. Check system memory
# Check available system memory
free -h
# Expected output:
# total used free shared buff/cache available
# Mem: 15Gi 8Gi 2Gi 1Gi 5Gi 6Gi
# If total memory is less than 4 GB, the Gradle daemon should use at most 1 GB
Prevention
- Set
org.gradle.jvmargs=-Xmx2048mas the default for all projects in~/.gradle/gradle.properties. - Enable parallel builds only if your system has 4+ CPU cores and 8+ GB RAM.
- Monitor daemon memory usage with
./gradlew --status. - Kill idle daemons with
./gradlew --stopafter large builds. - Use the
--no-daemonflag for CI/CD builds that don't benefit from daemon reuse.
Common Mistakes with daemon oom
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world GRADLE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro