Gradle Daemon Idle Timeout vs. Low-Traffic Build Schedules: Adaptive Retention Strategy
0 reputation · 27 Jun 2021, 16:36 UTC
0 reputation · 27 Jun 2021, 16:36 UTC
The Gradle Daemon lifecycle (governed by org.gradle.daemon.idletimeout) and sporadic build invocation patterns form an integration boundary where static configuration meets variable workload demand. The daemon terminates after a fixed idle period—defaulting to three hours—regardless of when the next build might arrive.
The objective is to minimize JVM startup and Gradle initialization latency for builds that occur hours or days apart, without permanently reserving heap and native memory on shared developer machines or CI agents. The current mechanism offers only a single static timeout value; there is no built-in adaptive policy that extends retention after frequent builds and shortens it during quiet periods. Experimental concepts such as predictive startup or daemon pooling remain unavailable in stable releases.
org.gradle.daemon.idletimeout at runtime based on recent build history without destabilizing concurrent daemon instances?Gradle's daemon idle timeout is a fixed value that is negotiated when the daemon starts. Once a daemon is running, its org.gradle.daemon.idletimeout cannot be changed by subsequent builds or by a plugin. Therefore an adaptive policy must be implemented outside the daemon: either by choosing a different timeout for each new daemon launch or by explicitly stopping daemons during predicted quiet windows.
org.gradle.daemon.idletimeout in gradle.properties or as a -D system property.-Dorg.gradle.daemon.idletimeout per invocation will cause a new daemon to be created if the value differs from any currently running daemon's timeout.Because the daemon can't be re-tuned at runtime, the adaptation must happen at launch time or via explicit --stop commands. A lightweight wrapper script can:
$GRADLE_USER_HOME/.gradle/build-timestamps.-Dorg.gradle.daemon.idletimeout=<computed-ms> only when a new daemon will be launched (i.e., when no matching daemon is found).On CI or scheduled environments, the timeout can be set directly from the known cron/trigger interval, bypassing the timestamp calculation. Additionally, a cron job or CI pre-step can run ./gradlew --stop during predictable quiet windows to ensure no daemon lingers beyond its intended life.
org.gradle.daemon.idletimeout mid-build will have no effect on the already running daemon and may unintentionally create a new one.# Check current daemon status
./gradlew --status
# Stop all daemons (useful after a quiet period)
./gradlew --stop
# Observe memory after idle period
jcmd <daemon-pid> VM.native_memory summary
To test the adaptive wrapper, set a short timeout (e.g., -Dorg.gradle.daemon.idletimeout=120000), run a build, wait 2 minutes, and confirm the daemon exits via --status.
To fine-tune the heuristic, could you share the typical distribution of your build intervals? Knowing whether builds cluster in hourly bursts or are spread over days will help decide the floor and percentile used for the timeout calculation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.