Kubernetes and JVM memory settings Kubernetes and JVM memory settings kubernetes kubernetes

Kubernetes and JVM memory settings


The default "max heap" if you don't specify -Xmx is 1/4 (25%) of the host RAM.JDK 10 improved support for containers in that it uses container's RAM limits instead of underlying host. As pointed by @David Maze this has been backported to JDK 8.

Assuming you have a sufficiently recent version of JDK 8, you can use -XX:MaxRAMPercentage to modify the default percentage of total RAM used for Max heap. So instead of specifying -Xmx you can tell, e.g. -XX:MaxRAMPercentage=75.0. See also https://medium.com/adorsys/usecontainersupport-to-the-rescue-e77d6cfea712

Here's an example using alpine JDK docker image: https://hub.docker.com/_/openjdk (see section "Make JVM respect CPU and RAM limits" in particular).

# this is running on the host with 2 GB RAMdocker run --mount type=bind,source="$(pwd)",target=/pwd -it openjdk:8# running with MaxRAMPercentage=50 => half of the available RAM is used as "max heap"root@c9b0b4d9e85b:/# java -XX:+PrintFlagsFinal -XX:MaxRAMPercentage=50.0 -version | grep -i maxheap    uintx MaxHeapFreeRatio                          = 100                                 {manageable}    uintx MaxHeapSize                              := 1044381696                          {product}openjdk version "1.8.0_265"OpenJDK Runtime Environment (build 1.8.0_265-b01)OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)# running without MaxRAMPercentage => default 25% of RAM is usedroot@c9b0b4d9e85b:/# java -XX:+PrintFlagsFinal -version | grep -i maxheap    uintx MaxHeapFreeRatio                          = 100                                 {manageable}    uintx MaxHeapSize                              := 522190848                           {product}openjdk version "1.8.0_265"