# 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 these 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 the 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, you won't want to go back.

The Tooling API (see Chapter 63, 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 the future, there are plans for more features in the daemon:

- 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 a new daemon process is forked read the dedicated section below. The daemon process automatically expires after 3 hours of idle time.

Here are 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 a 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 the -Xmx memory setting changed or some fundamental immutable system property changed (e.g. file.encoding) then a new daemon will be forked.
- At the moment the daemon is coupled with a 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: this command will only stop daemons that were started with Gradle version that is executing `--stop`.

We plan to improve the functionality of managing and pooling the daemons in the future.

## 19.3. Usage and troubleshooting

For command line usage, look at the 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”. This 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”