Configuring JVM settings on Openshift

In this tutorial we will learn how to configure JVM settings for WildFly Container running on Openshift. More in detail, we will see how to apply Compute resource constraints to shape the JVM memory settings.

The recommended option to set JVM memory settings for WildFly / JBoss EAP is to use resource constraints. Resource constraints can be used to set both the amount of memory to assign to the JVM process and how much CPU. There are several ways to define Compute resource constraints:

  • Via Pod
  • Via Deployment Configurations
  • Via Templates
  • Via Project limit ranges

Here is a sample Compute Resource Constraints we can apply to our Deployment Config:

  resources:
    limits:
      memory: 2G
    requests:
      memory: 2G

As you can see, there are two type of Memory Resource constraints:

1) Memory Requests

By default, a container is able to consume as much memory on the node as possible. In order to improve placement of pods in the cluster, specify the amount of memory required for a container to run. The scheduler will then take available node memory capacity into account prior to binding your pod to a node. A container is still able to consume as much memory on the node as possible even when specifying a request.

2) Memory Limits

If you specify a memory limit, you can constrain the amount of memory the container can use. For example, if you specify a limit of 200Mi, a container will be limited to using that amount of memory on the node. If the container exceeds the specified memory limit, it will be terminated and potentially restarted dependent upon the container restart policy.

When you change the deployment settings with the above resource limits you will see that the following -Xms and -Xmx have been used:

  JAVA_OPTS: -javaagent:"/opt/wildfly/jboss-modules.jar"  -server -Xms238m -Xmx954m -XX:MetaspaceSize=96m -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=jdk.nashorn.api,com.sun.crypto.provider -Djava.awt.headless=true -XX:+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError -Djava.security.egd=file:/dev/./urandom  -Djboss.modules.settings.xml.url=file:///opt/jboss/container/wildfly/s2i/galleon/settings.xml  --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED --add-exports=jdk.unsupported/sun.reflect=ALL-UNNAMED

As a matter of fact, the heap memory size is a portion of the total memory in a container. So, setting a limit of 2G will grant approximately 50% of that memory.

In order to change the minimum memory size, you can give bigger JAVA_INITIAL_MEM_RATIO  as a DeploymentConfig environment variable.

oc set env dc/<DC_NAME> JAVA_INITIAL_MEM_RATIO=40

You can also increase the maximum memory size but you should keep in mind that the container needs extra memory for metadata, thread, code cache, etc. Therefore, you have to take caution when adjusting the JAVA_MAX_MEM_RATIO.

oc set env dc/<DC_NAME> JAVA_MAX_MEM_RATIO=65

And here is the new memory settings after setting JAVA_INITIAL_MEM_RATIO and JAVA_MAX_MEM_RATIO:

  JAVA_OPTS: -javaagent:"/opt/wildfly/jboss-modules.jar"  -server -Xms496m -Xmx1240m -XX:MetaspaceSize=96m -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=jdk.nashorn.api,com.sun.crypto.provider -Djava.awt.headless=true -XX:+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError -Djava.security.egd=file:/dev/./urandom  -Djboss.modules.settings.xml.url=file:///opt/jboss/container/wildfly/s2i/galleon/settings.xml  --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED --add-exports=jdk.unsupported/sun.reflect=ALL-UNNAMED

Setting other JVM arguments

As far as other JVM System Properties are concerned, you can set them through the JAVA_OPTS_APPEND environment variable as follows:

oc set env dc/<DC_NAME> JAVA_OPTS_APPEND=-Dmy.property=1
Found the article helpful? if so please follow us on Socials