Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting JVM maximum heap size when running Kotlin scripts?

Tags:

kotlin

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
like image 483
Dave Challis Avatar asked Jul 01 '19 10:07

Dave Challis


People also ask

How do I set JVM heap size?

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.

What is the maximum recommended JVM heap size?

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.

Is it good to set the max and min JVM heap size the same?

“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.”

Which JVM parameter sets maximum heap size?

-Xmx to specify the maximum heap size.


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"
like image 108
Dave Challis Avatar answered Nov 15 '22 12:11

Dave Challis