Setting JVM maximum heap size when running Kotlin scripts?

Viewed 2359

I'm running a Kotlin script using:

kotlinc -script myscript.kts

This does some pretty heavy data processing, and quickly runs into GC/heap space errors (e.g. java.lang.OutOfMemoryError: GC overhead limit exceeded or java.lang.OutOfMemoryError: Java heap space depending on what the script processes).

Is there a way to increase the JVM heap size when running Kotlin scripts? An attempt to use:

kotlinc -Xmx8g -script myscript.kts

gives the warning:

warning: flag is not supported by this version of the compiler: -Xmx8g
1 Answers

After taking a look at the actual kotlinc bash script, the heap sizing options are set based on an environment variable by the line:

[ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx256M -Xms32M"

Setting this environment variable before executing the kotlin script works to increase the heap size.

For the example above, the following works on Linux/MacOs:

$ export JAVA_OPTS="-Xmx8g"
$ kotlinc -script myscript.kts

On Windows:

set JAVA_OPTS="-Xmx8g"
Related