Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Kotlin Gradle build die with exit code 137?

Tags:

gradle

kotlin

We were seeing mysterious failures in our CI environment when running tests of a Kotlin code base.

gradle test compiled the code and the tests just fine. The tests ran and all appeared to pass. But then Gradle exited with code 137 (indicating it was killed with SIGKILL) and the CI system thus thought the build had failed.

Limiting the size of the JVM for Gradle didn't help. Nor did the --no-daemon and --max-workers options.

This is using Kotlin 1.2.40 and Gradle 4.3 on the Java 8 JVM.

like image 319
Steven Grimm Avatar asked Oct 15 '25 09:10

Steven Grimm


1 Answers

The culprit in this case turned out to be the Kotlin compiler.

By default, the Kotlin compiler spawns a daemon in the background so that subsequent compilation jobs are faster. For a Kotlin code base of nontrivial size, this process can end up eating significant memory.

Its presence was causing Gradle to hit the memory limit on the CI container, and the Linux OOM killer was killing the Gradle process.

The solution: Tell the Kotlin compiler not to spawn the background process. This turns out to be a simple matter of setting an environment variable:

GRADLE_OPTS=-Dkotlin.compiler.execution.strategy=in-process

With that variable in place, Kotlin compilation runs inline in the Gradle JVM, and its data can thus be garbage-collected when Gradle moves on to running the tests.

It would also work to pass the option to the gradle command rather than setting it in the environment.

like image 120
Steven Grimm Avatar answered Oct 18 '25 07:10

Steven Grimm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!