|
| 1 | +[//]: # (title: Kotlin daemon) |
| 2 | + |
| 3 | +The Kotlin daemon is a background process that build systems can use to improve build times by keeping the compiler |
| 4 | +and its environment ready to compile. This approach avoids starting a new Java Virtual Machine (JVM) |
| 5 | +instance and reinitializing the compiler for every compilation, leading to reduced build times for incremental builds |
| 6 | +or frequent small changes. |
| 7 | + |
| 8 | +Some build systems have their own daemons that help reduce startup costs, such as the [Gradle daemon](https://docs.gradle.org/current/userguide/gradle_daemon.html) and the [Maven daemon](https://maven.apache.org/tools/mvnd.html). |
| 9 | +Using the Kotlin daemon instead reduces startup costs while also completely isolating the build system process from the compiler. |
| 10 | +This separation is useful in dynamic environments where system settings might change at runtime. |
| 11 | + |
| 12 | +Although the Kotlin daemon has no direct user-facing interface, you can use it through build systems or the [build tools API](build-tools-api.md). |
| 13 | + |
| 14 | +## Kotlin daemon configuration |
| 15 | + |
| 16 | +There are ways to configure some settings for the Kotlin daemon for Gradle or Maven. |
| 17 | + |
| 18 | +### Memory management |
| 19 | + |
| 20 | +The Kotlin daemon is a separate process that has its own memory space, isolated from the client. |
| 21 | +By default, the Kotlin daemon tries to inherit the heap size (`-Xmx`) of the launching JVM process. |
| 22 | + |
| 23 | +To configure specific memory limits, like `-Xmx` and `-XX:MaxMetaspaceSize`, use the following property: |
| 24 | + |
| 25 | +<tabs group="build-system"> |
| 26 | +<tab title="Gradle" group-key="gradle"> |
| 27 | + |
| 28 | +```kotlin |
| 29 | +kotlin.daemon.jvmargs=-Xmx1500m |
| 30 | +``` |
| 31 | + |
| 32 | +For more information, see [`kotlin.daemon.jvmargs` property](gradle-compilation-and-caches.md#kotlin-daemon-jvmargs-property). |
| 33 | + |
| 34 | +</tab> |
| 35 | +<tab title="Maven" group-key="maven"> |
| 36 | + |
| 37 | +```xml |
| 38 | +<kotlin.compiler.daemon.jvmArgs>-Xmx1500m</kotlin.compiler.daemon.jvmArgs> |
| 39 | +``` |
| 40 | + |
| 41 | +</tab> |
| 42 | +</tabs> |
| 43 | + |
| 44 | +### Lifetime |
| 45 | + |
| 46 | +There are two common lifetime strategies for the Kotlin daemon: |
| 47 | + |
| 48 | +* **Attached daemon**: Shut down the daemon shortly after the client process shuts down or the daemon hasn't been used for a while. Use when the client is long-running. |
| 49 | +* **Detached daemon**: Keep the daemon alive longer to await potential follow-up requests. Use when the client is short-lived. |
| 50 | + |
| 51 | +To configure the lifetime strategy, you can use the following options: |
| 52 | + |
| 53 | +| Option | Description | Default value | |
| 54 | +|-----------------------------|----------------------------------------------------------------------------------------------------|---------------| |
| 55 | +| `autoshutdownIdleSeconds` | How long the daemon should stay alive after the last compilation when a client is still connected. | 2 hours | |
| 56 | +| `autoshutdownUnusedSeconds` | How long a newly started daemon waits for a first client before shutting down if unused. | 1 minute | |
| 57 | +| `shutdownDelayMilliseconds` | How long the daemon waits to shut down after all clients disconnect. | 1 second | |
| 58 | + |
| 59 | +To configure an attached daemon lifetime strategy, set `autoshutdownIdleSeconds` to a **high** value and `shutdownDelayMilliseconds` to a **low** value. |
| 60 | + |
| 61 | +<tabs group="build-system"> |
| 62 | +<tab title="Gradle" group-key="gradle"> |
| 63 | + |
| 64 | +Add the following to your `gradle.properties` file: |
| 65 | + |
| 66 | +```none |
| 67 | +org.gradle.jvmargs=-Dkotlin.daemon.jvm.options=autoshutdownIdleSeconds=7200,shutdownDelayMilliseconds=1000 |
| 68 | +``` |
| 69 | + |
| 70 | +</tab> |
| 71 | +<tab title="Maven" group-key="maven"> |
| 72 | + |
| 73 | +Use the following command: |
| 74 | + |
| 75 | +```bash |
| 76 | + mvn package -Dkotlin.daemon.options=autoshutdownIdleSeconds=7200,shutdownDelayMilliseconds=1000 |
| 77 | +``` |
| 78 | + |
| 79 | +</tab> |
| 80 | +</tabs> |
| 81 | + |
| 82 | +To configure a detached daemon lifetime strategy, set `shutdownDelayMilliseconds` to a **high** value. |
| 83 | + |
| 84 | +<tabs group="build-system"> |
| 85 | +<tab title="Gradle" group-key="gradle"> |
| 86 | + |
| 87 | +Add the following to your `gradle.properties` file: |
| 88 | + |
| 89 | +```none |
| 90 | +org.gradle.jvmargs=-Dkotlin.daemon.jvm.options=shutdownDelayMilliseconds=7200 |
| 91 | +``` |
| 92 | + |
| 93 | +</tab> |
| 94 | +<tab title="Maven" group-key="maven"> |
| 95 | + |
| 96 | +Add the following property to your `pom.xml` file: |
| 97 | + |
| 98 | +```xml |
| 99 | +<kotlin.compiler.daemon.shutdownDelayMs>7200</kotlin.compiler.daemon.shutdownDelayMs> |
| 100 | +``` |
| 101 | + |
| 102 | +</tab> |
| 103 | +</tabs> |
0 commit comments