← Guides

Gradle daemon memory on a Mac

After a build, Gradle leaves a daemon JVM running so the next build starts fast. It stays up for 3 idle hours, Kotlin projects leave a Kotlin compile daemon next to it, and a JVM rarely hands memory back once its heap has grown. Two or three projects can mean several gigabytes doing nothing.

See what's running

From a project folder, ./gradlew --status lists the daemons, but only those running that project's Gradle version. To see all of them with their memory:

ps -axo pid=,rss=,command= | awk '/[G]radleDaemon|[K]otlinCompileDaemon/ {printf "%6s %6.0f MB  %s\n", $1, $2/1024, (/Kotlin/ ? "Kotlin daemon" : "Gradle daemon")}'

You'll often find more than one per project. Gradle starts a new daemon whenever the Gradle version, the JDK or org.gradle.jvmargs differs from the running ones.

Stop them

# daemons of this project's Gradle version
./gradlew --stop

# every Gradle and Kotlin daemon, any version
pkill -f GradleDaemon
pkill -f KotlinCompileDaemon

--stop leaves daemons from other Gradle versions alone, which is why memory often doesn't drop as much as you expect. pkill sends SIGTERM, so the daemons shut down cleanly. Don't run either in the middle of a build: it fails, and the next build starts a fresh daemon.

Make them smaller

Put these in ~/.gradle/gradle.properties to cover every project. Values there override each project's own gradle.properties.

# Gradle daemon heap. Gradle's own default is 512 MB; Android project templates set 2 GB.
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -Dfile.encoding=UTF-8

# Exit after 1 idle hour instead of 3. The value is in milliseconds.
org.gradle.daemon.idletimeout=3600000

# Kotlin daemon heap. Without this it copies -Xmx from the Gradle daemon.
kotlin.daemon.jvmargs=-Xmx1g

Too small a heap makes builds slow or fail with OutOfMemoryError. 2 GB for Gradle and 1 GB for Kotlin suit most mid-size Android apps; raise them if builds start failing. The Kotlin daemon also exits when the Gradle daemon does, or after 2 hours without compiling.