Java Memory commands

Java Heap space is used by the Java runtime to allocate memory to Objects and JRE classes. Whenever we create an object, it’s always created in the Heap space. This post, we’ll explain to how to set the memory size on Platform.sh.

Garbage Collection runs on the heap memory to free the memory used by objects that don’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere in the application.

To set the memory size on JVM on Platform.sh we need to append the memory settings with the Java startup parameter.

Platform.sh has a command that returns the memory that is available with -$(jq .info.limits.memory /run/config.json) that value is on megabytes. As a recommendation, you can use the fine maximum JVM memory.

web:
    commands:
        start: java -jar -Xmx$(jq .info.limits.memory /run/config.json)m target/microprofile-microbundle.jar --port $PORT

Below, more arguments to set the memory:

-Xmn size

Sets the initial and maximum size (in bytes) of the heap for the young generation (nursery). Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, or g or G to indicate gigabytes. The young generation region of the heap is used for new objects. GC is performed in this region more often than in other regions. If the size for the young generation is too small, then a lot of minor garbage collections are performed. If the size is too large, then only full garbage collections are performed, which can take a long time to complete. Oracle recommends that you keep the size for the young generation greater than 25% and less than 50% of the overall heap size.

#-Xms size

Sets the initial size (in bytes) of the heap. This value must be a multiple of 1024 and greater than 1 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes.

-Xmx size

Specifies the maximum size (in bytes) of the memory allocation pool in bytes. This value must be a multiple of 1024 and greater than 2 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, and g or G to indicate gigabytes. The default value is chosen at runtime based on system configuration. For server deployments, -Xms and -Xmx are often set to the same value.

As mentioned, previously use the $(jq .info.limits.memory /run/config.json) to create the Xmx command. E.g.: java -jar -Xmx$(jq .info.limits.memory /run/config.json)m

-Xnoclassgc

Disables garbage collection (GC) of classes. This can save some GC time, which shortens interruptions during the application run. When you specify -Xnoclassgc at startup, the class objects in the application are left untouched during GC and are always considered live.

-Xss size

Sets the thread stack size (in bytes). Append the letter k or K to indicate KB, m or M to indicate MB, and g or G to indicate GB.

ExitOnOutOfMemoryError

When you enable this option, the JVM exits on the first occurrence of an out-of-memory error. It can be used if you prefer restarting an instance of the JVM rather than handling out of memory errors.

Recommended Java memory command at Platform.sh

java -jar -Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError ...

GC Log activated

java -jar -Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError -XX:+PrintGCDetails ...

References