Chapter 19. The Gradle Daemon

19.1. Enter the daemon

The Gradle daemon (sometimes referred as the build daemon) aims to improve the startup and execution time of Gradle.

We came up with several use cases where the daemon is very useful. For some workflows, the user invokes Gradle many times to execute a small number of relatively quick tasks. For example:

  • When using test driven development, where the unit tests are executed many times.
  • When developing a web application, where the application is assembled many times.
  • When discovering what a build can do, where gradle tasks is executed a number of times.

For above sorts of workflows, it is important that the startup cost of invoking Gradle is as small as possible.

In addition, user interfaces can provide some interesting features if the Gradle model can be built relatively quickly. For example, the daemon might be useful for following scenarios:

  • Content assistance in the IDE
  • Live visualisation of the build in a GUI
  • Tab completion in a CLI

In general, snappy behavior of the build tool is always handy. If you try using the daemon for your local builds it's going to be hard for you to go back to regular use of Gradle.

The Tooling API (see Chapter 62, Embedding Gradle) uses the daemon all the time, e.g. you cannot officially use the Tooling API without the daemon. This means that whenever you are using the STS Gradle plugin for Eclipse or the Gradle support in Intellij IDEA, you're already using the Gradle Daemon.

In future the daemon will offer more features:

  • Snappy up-to-date checks: use native file system change notifications (e.g. via jdk7 nio.2) to preemptively perform up-to-date analysis.
  • Even faster builds: preemptively evaluate projects, so that the model is ready when the user next invokes Gradle.
  • Did we mention faster builds? The daemon can potentially preemptively download dependencies or check for new versions of snapshot dependencies.
  • Utilize a pool of reusable processes available for compilation and testing. For example, both the Groovy and Scala compilers have a large startup cost. The build daemon could maintain a process with Groovy and/or Scala already loaded.
  • Preemptive execution of certain tasks, for example compilation. Quicker feedback.
  • Fast and accurate bash tab completion.
  • Periodically garbage collect the Gradle caches.

19.2. Reusing and expiration of daemons

The basic idea is that the gradle command forks a daemon process, which performs the actual build. Subsequent invocations of the gradle command will reuse the daemon, avoiding the startup costs. Sometimes we cannot use an existing daemon because it is busy or its java version or jvm arguments are different. For exact details on when exactly new daemon process is forked read the dedicated section below. The daemon process automatically expire after 3 hours of idle time.

Here're all situations in which we fork a new daemon process:

  • If the daemon process is currently busy running some job, a brand new daemon process will be started.
  • We fork a separate daemon process per java home. So even if there is some idle daemon waiting for build requests but you happen to run build with a different java home then a brand new daemon will be forked.
  • We fork a separate daemon process if the jvm arguments for the build are sufficiently different. For example we will not fork a new daemon if a some system property has changed. However if -Xmx memory setting change or some fundamental immutable system property changes (e.g. file.encoding) then new daemon will be forked.
  • At the moment daemon is coupled with particular version of Gradle. This means that even if some daemon is idle but you are running the build with a different version of Gradle, a new daemon will be started. This also has a consequence for the --stop command line instruction: You can only stop daemons that were started with the Gradle version you use when running --stop.

We plan to improve the ways of managing / pooling the daemons in future.

19.3. Usage and troubleshooting

For command line usage take a look dedicated section in Appendix D, Gradle Command Line. If you are tired of using the same command line options again and again, take a look at Section 20.1, “Configuring the build environment via gradle.properties”. The section contains information on how to configure certain behavior of the daemon (including turning on the daemon by default) in a more 'persistent' way.

Some ways of troubleshooting the Gradle daemon:

  • If you have a problem with your build, try temporarily disabling the daemon (you can pass the command line switch --no-daemon).
  • Occasionally, you may want to stop the daemons either via the --stop command line option or in a more forceful way.
  • There is a daemon log file, which by default is located in the Gradle user home directory.
  • You may want to start the daemon in --foreground mode to observe how the build is executed.

19.4. Configuring the daemon

Some daemon settings, such as JVM arguments, memory settings or the Java home, can be configured. Please find more information in Section 20.1, “Configuring the build environment via gradle.properties”