Why is Gradle build of Kotlin source code pausing

2019-08-04 08:01发布

问题:

Every build takes an extra 3-4 seconds, pausing immediately after the log output prints the following.

[LIFECYCLE] [org.jetbrains.kotlin.gradle.plugin.KotlinGradleBuildServices] Forcing System.gc()

Why is it "forcing" this? How do I avoid this and speed up my build?

回答1:

I've looked into this, and this is a consequence of having Gradle's debug-level logging enabled (eg. gradle --debug assemble).

Run Gradle without debug logging enabled (eg. gradle --info assemble) and this should not occur anymore.

References: libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinGradleBuildServices.kt



回答2:

Kotlin Gradle plugins calls System.gc before and after a build only when debug logging is enabled (Gradle is run with -d or --debug command line argument). Users do not normally run Gradle with debug logging enabled because it is extremely noisy and slow, so forcing a GC is a relatively minor issue.

Historically this behaviour was added to test against memory leaks when Gradle daemon is enabled. The idea was to log a difference of used memory before and after a build, run a few builds consequently in a test, and assert that the difference is not exceeding the threshold.

I think calling System.gc should be avoided unless the test KotlinGradleIT#testKotlinOnlyDaemonMemory is running, so I've created an issue at Kotlin bugtracker https://youtrack.jetbrains.com/issue/KT-17960