Most of the time, we set these parameters Xms and Xmx for setting up the initial heap size and maximum heap size for our java application.
In JAVA 8, they introduced new parameters that allow us to specify a percentage value.
-XX:InitialRAMPercentage -> Set initial heap size as a percentage of total memory.
-XX: MaxRAMPercentage -> Set Maximum heap size as a percentage of total memory.
Let’s say a java container is running and its memory size is 1GB, then if you want to set the initial heap size to ~250 MB and the max heap size to ~500 MB and also you don’t want to calculate these values by yourself and let the JVM handle these, you should set the XX:InitialRAMPercentage as 25.00 and XX:MaxRAMPercentage as 50.00
docker run -m 1GB openjdk:8 java -XX:MaxRAMPercentage=50.0 -XX:InitialRAMPercentage=25.0 -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize'
The output is given below
uintx ErgoHeapSizeLimit = 0 {product}
uintx HeapSizePerGCThread = 87241520 {product}
uintx InitialHeapSize := 268435456 {product}
uintx LargePageHeapSizeThreshold = 134217728 {product}
uintx MaxHeapSize := 536870912 {product}
openjdk version "1.8.0_332"
OpenJDK Runtime Environment (build 1.8.0_332-b09)
OpenJDK 64-Bit Server VM (build 25.332-b09, mixed mode)
We have another parameter called MinRAMPercentage which is used to set the maximum heap size for a JVM running with a small amount of memory(Less than 200 MB).
If a JVM is running with less than 200 MB, then we don’t have to set the MaxRAMPercentage, and setting the MinRAMPercentage is more than enough. Let’s see an example.
docker run -m 200m openjdk:8 java -XX:MaxRAMPercentage=90.0 -XX:MinRAMPercentage=50.0 -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize'
The output is given below
uintx ErgoHeapSizeLimit = 0 {product}
uintx HeapSizePerGCThread = 87241520 {product}
uintx InitialHeapSize := 8388608 {product}
uintx LargePageHeapSizeThreshold = 134217728 {product}
uintx MaxHeapSize := 104857600 {product}
openjdk version "1.8.0_332"
OpenJDK Runtime Environment (build 1.8.0_332-b09)
OpenJDK 64-Bit Server VM (build 25.332-b09, mixed mode)
If you look at the above example, we set the MaxRAMPercentage as 90, so it should suppose to assign the max heap size to more than 130 MB but the value is 104 MB only. It’s because the MaxRAMPercentage is not going to be used when the JVM memory is less than 200 MB. So only the MinRAMPercentage parameter is used.
Refer to the below example:
docker run -m 200m openjdk:8 java -XX:MinRAMPercentage=90.0 -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize'
uintx ErgoHeapSizeLimit = 0 {product}
uintx HeapSizePerGCThread = 87241520 {product}
uintx InitialHeapSize := 8388608 {product}
uintx LargePageHeapSizeThreshold = 134217728 {product}
uintx MaxHeapSize := 132120576 {product}
openjdk version "1.8.0_332"
OpenJDK Runtime Environment (build 1.8.0_332-b09)
OpenJDK 64-Bit Server VM (build 25.332-b09, mixed mode)
We set the XX:MinRAMPercentage to 90.0 and because of that, the max heap size is set to 132 MB. Please be aware that not the whole memory will be allocated to the heap.