JVM

Introduction

TBD

Docker container usual flags

With JDK 21

Setting initial heap size and max heap the same will incur lower Garbage Collection pause times.

‘-XX:+UseContainerSupport’ is passed by default argument in the JVM. So, you don’t need to configure it explicitly.

-XX:+ExitOnOutOfMemoryError -XX:MaxMetaspaceSize=200m -XX:MaxRAMPercentage=70.0 -XX:MinRAMPercentage=50.0 -XX:InitialRAMPercentage=70.0 -XX:+PrintFlagsFinal -XX:-OmitStackTraceInFastThrow

With JDK 17

-XX:+ExitOnOutOfMemoryError -XX:MaxMetaspaceSize=200m -XX:MaxRAMPercentage=50.0 -XX:MinRAMPercentage=50.0 -XX:+PrintFlagsFinal -XX:+UseContainerSupport

With JDK 11

-XX:+ExitOnOutOfMemoryError -XX:MaxMetaspaceSize=200m -XX:MaxRAMPercentage=50.0 -XX:MinRAMPercentage=50.0 -XX:+PrintFlagsFinal -XX:+UseContainerSupport

With JDK 8

n/a

With JDK 7

Sample JBoss w/ Java 7 running in a 2048 MB machine:

-Xmx1024m -Xms512m -XX:MaxPermSize=512m -XX:OnOutOfMemoryError=\"kill -9 %p\"

Runtime Options for Java

The reference "VM Options Explorer" allows to find out if an options is supported by an specific JDK distribution.


-Xms

Minimum heap size. 

-Xmx

Supported in all versions of Java.

Maximum heap size. Using the ‘-Xmx’ JVM argument, you specify fine-grained specific size such as 512MB, 1024MB.

Eg:1

$ docker run -m 1GB openjdk:21 java -Xmx512m -XshowSettings:vm -version

VM settings:

    Max. Heap Size: 512.00M

    Using VM: OpenJDK 64-Bit Server VM

-XX:+ExitOnOutOfMemoryError

>=jdk7u101 & >=jdk8u92

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.

-XX:MaxMetaspaceSize=size 

Sets the maximum amount of native memory that can be allocated for class metadata. By default, the size isn’t limited. The amount of metadata for an application depends on the application itself, other running applications, and the amount of memory available on the system.

The following example shows how to set the maximum class metadata size to 256 MB:

-XX:MaxMetaspaceSize=256m

-XX:MaxRAMPercentage=percent 

Supported from Java 8 update 191 and above.
Sets the maximum amount of memory that the JVM may use for the Java heap before applying ergonomics heuristics as a percentage of the maximum amount determined as described in the -XX:MaxRAM option. The default value is 25 percent.

Specifying this option disables automatic use of compressed oops if the combined result of this and other options influencing the maximum amount of memory is larger than the range of memory addressable by compressed oops. See -XX:UseCompressedOops for further information about compressed oops.

The following example shows how to set the percentage of the maximum amount of memory used for the Java heap:

-XX:MaxRAMPercentage=75

-XX:MinRAMPercentage=percent 

Supported from Java 8 update 191 and above.

Sets the maximum amount of memory that the JVM may use for the Java heap before applying ergonomics heuristics as a percentage of the maximum amount determined as described in the -XX:MaxRAM option for small heaps. A small heap is a heap of approximately 125 MB. The default value is 50 percent.

The following example shows how to set the percentage of the maximum amount of memory used for the Java heap for small heaps:

-XX:MinRAMPercentage=75

-XX:+PrintFlagsFinal

It prints all options and their values used by the JVM. 

-XX:-UseContainerSupport

The VM now provides automatic container detection support, which allows the VM to determine the amount of memory and number of processors that are available to a Java process running in docker containers. It uses this information to allocate system resources. This support is only available on Linux x64 platforms. If supported, the default for this flag is true, and container support is enabled by default. It can be disabled with -XX:-UseContainerSupport.

Unified Logging is available to help to diagnose issues related to this support.

Use -Xlog:os+container=trace for maximum logging of container information. See Enable Logging with the JVM Unified Logging Framework for a description of using Unified Logging.

-XX:+UseStringDeduplication

Enables string deduplication. By default, this option is disabled. To use this option, you must enable the garbage-first (G1) garbage collector.

String deduplication reduces the memory footprint of String objects on the Java heap by taking advantage of the fact that many String objects are identical. Instead of each String object pointing to its own character array, identical String objects can point to and share the same character array.