The daemon and faster builds
Starting a JVM, loading Gradle and compiling the build scripts takes a second or two of every build. The daemon is a JVM Gradle leaves running with all of that done, so the next build starts at the task graph instead of at the beginning. This lesson shows the daemon, the reason every command of this course turns it off, and the properties worth setting in a real project.
What the daemon is
The first build of a project starts a background process, hands it the command and waits. The process keeps the Gradle classes loaded, the build logic compiled and the caches warm, and it stops itself after three hours without work. A second build in the same project reaches the task graph in a fraction of the time of the first.
The price is a process that outlives the terminal. It holds memory, it can hold file locks of a folder that was deleted, and it runs with the settings of the JVM it was started with, so a change to org.gradle.jvmargs needs a stop before it takes effect.
gradle --status
The command is gradle --status. On a machine that has run no build with a daemon it answers with one line, and the line below is that answer; on a machine where a build is running it lists the process instead, which is why the block is quoted and not compared with a run.
No Gradle daemons are running.
Only Daemons for the current Gradle version are displayed. For more on this, please refer to https://docs.gradle.org/9.9.2/userguide/gradle_daemon.html#sec:status in the Gradle documentation.
gradle --stop stops every daemon of the same version, and --no-daemon runs one build in a process that ends with the build. On this machine the second form is what every command of the course uses.
Why this image disables it
A container that is created for one build and thrown away has nothing to gain from a daemon: the process is started and stopped inside the same second, and a leftover one is memory the next container does not have. The pages of this course therefore show --no-daemon, and the same run in a terminal on a developer machine prints two lines that the output blocks leave out:
To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/9.9.2/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation.
Daemon will be stopped at the end of the build
They are the tool saying that it needs a JVM with other settings than the one it was started in, and that the process is temporary. They belong to the machine, not to the build, which is why every output block of this course is an excerpt without them.
The performance properties
gradle.properties
# The JVM of the build, not of the launcher.
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m
# Run independent tasks of different projects at the same time.
org.gradle.parallel=true
# Keep a cache of tasks and configuration between builds.
org.gradle.caching=true
org.gradle.configuration-cache=true
Three more settings help a large build and cost nothing in a small one: org.gradle.configureondemand configures only the projects a command touches, which a convention plugin makes cheap; org.gradle.workers.max caps the threads of parallel work; and org.gradle.priority=low keeps a build from taking a workstation while somebody types. The previous lesson takes the two caches apart.
Related
- Build and configuration caching: the two switches of the block above.
- The task graph: what --parallel can run at the same time.
- Gradle in CI and Docker: why a CI job runs without a daemon too.
Article author: Arthur Isaev