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
Under the Java tab, select JVM Options. Edit the -Xmx256m option. This option sets the JVM heap size. Set the -Xmx256m option to a higher value, such as Xmx1024m.
The theoretical limit is 2^64 bytes, which is 16 exabytes (1 exabyte = 1024 petabytes, 1 petabyte = 1024 terabytes). However, most OS's can't handle that. For instance, Linux can only support 64 terabytes of data. Note: We don't recommend you exceed 2 GB of in use JVM heap.
“If the initial heap is too small, the Java application startup becomes slow as the JVM is forced to perform garbage collection frequently until the heap grows to a more reasonable size. For optimal startup performance, set the initial heap size to be the same as the maximum heap size.”
-Xmx to specify the maximum heap size.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With