JVM Memory Settings in a Container Environment

The Bare Minimum You Should Know Before Going Live

Roman Ataman
Nov 10, 2018 · 9 min read
Image for post
Image for post
Photo by Kyle Ryan on Unsplash

UPDATE: Things Have Changed for Java 8u191+ and 10+

TL;DR

Java Heap Sizing Basics

$ java -XX:+PrintFlagsFinal -version | grep -Ei "maxheapsize|maxram"
uintx DefaultMaxRAMFraction = 4 {product}
uintx MaxHeapSize := 8589934592 {product}
uint64_t MaxRAM = 137438953472 {pd product}
uintx MaxRAMFraction = 4 {product}
$ java -XX:+PrintFlagsFinal -Xmx1g -version | grep -Ei "maxheapsize|maxram"

uintx DefaultMaxRAMFraction = 4 {product}
uintx MaxHeapSize := 1073741824 {product}
uint64_t MaxRAM = 137438953472 {pd product}
uintx MaxRAMFraction = 4 {product}
$ java -XX:+PrintFlagsFinal -XX:MaxRAM=1g -version | grep -Ei "maxheapsize|maxram"
uintx DefaultMaxRAMFraction = 4 {product}
uintx MaxHeapSize := 268435456 {product}
uint64_t MaxRAM := 1073741824 {pd product}
uintx MaxRAMFraction = 4 {product}
+----------------+-------------------+
| MaxRAMFraction | % of RAM for heap |
|----------------+-------------------|
| 1 | 100% |
| 2 | 50% |
| 3 | 33% |
| 4 | 25% |
+----------------+-------------------+
# -XX:MaxRAM is only set for the sake of this example to simulate a smaller physical machine
$ java -XX:+PrintFlagsFinal -XX:MaxRAM=1g -XX:MaxRAMFraction=2 -version | grep -Ei "maxheapsize|maxram"
uintx DefaultMaxRAMFraction = 4 {product}
uintx MaxHeapSize := 536870912 {product}
uint64_t MaxRAM := 1073741824 {pd product}
uintx MaxRAMFraction := 2 {product}

Java Heap Sizing in Containers

$ docker run --rm alpine free -m
total used free shared buffers cached
Mem: 1998 1565 432 0 8 1244
$ docker run --rm -m 256m alpine free -m
total used free shared buffers cached
Mem: 1998 1552 445 1 8 1244
$ docker run --rm -m 1g openjdk:8-jdk cat /sys/fs/cgroup/memory/memory.limit_in_bytes
1073741824
$ docker run --rm -m 1g openjdk:8-jdk sh -c "java -XX:+PrintFlagsFinal -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -version | grep -Ei 'maxheapsize|maxram'"
uintx DefaultMaxRAMFraction = 4 {product}
uintx MaxHeapSize := 268435456 {product} # = 1073741824 / 4
uint64_t MaxRAM = 137438953472 {pd product}
uintx MaxRAMFraction = 4 {product}

Better Ways to Configure Heap

# set -XX:MaxRAM to 70% of the cgroup limit
docker run --rm -m 1g openjdk:8-jdk sh -c 'exec java -XX:MaxRAM=$(( $(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) * 100 / 70 )) -XX:+PrintFlagsFinal -version'

References

How we Tested

$ use-java10
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
$ java -version
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment 18.3 (build 10.0.2+13)
OpenJDK 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

$ use-java8
JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
$ java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (Zulu 8.31.0.1-macosx) (build 1.8.0_181-b02)
OpenJDK 64-Bit Server VM (Zulu 8.31.0.1-macosx) (build 25.181-b02, mixed mode)

Further Reading

adorsys

adorsys entwickelt innovative Softwarelösungen für…

Roman Ataman

Written by

software developer & architect

adorsys

adorsys

adorsys entwickelt innovative Softwarelösungen für Unternehmen. Dazu gehören webbasierte und mobile Anwendungen für alle Endgeräte. Darüber hinaus lassen wir unsere Kunden auch nach der Implementierung nicht allein.

Roman Ataman

Written by

software developer & architect

adorsys

adorsys

adorsys entwickelt innovative Softwarelösungen für Unternehmen. Dazu gehören webbasierte und mobile Anwendungen für alle Endgeräte. Darüber hinaus lassen wir unsere Kunden auch nach der Implementierung nicht allein.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store