docker / labs

This is a collection of tutorials for learning how to use Docker with various tools. Contributions welcome.
Apache License 2.0
11.53k stars 5.3k forks source link

Best practices for JVM memory management in Docker containers #361

Open jetztgradnet opened 6 years ago

jetztgradnet commented 6 years ago

Which lab is it that you're having issues with? Lab: Docker for Java Developers

Description

When deploying Java applications in production one usually specifies the amount of memory available to the JVM (-Xmx) and other assorted configuration settings. Unfortunately, this is (usually) static configuration and therefore fixed in the container image. When specifying memory constraints (using cgroups) this is unfortunately not picked up by the JVM automatically, so there should be a section on memory management for dockerized Java apps.

Java9 and Java8 > u131 provide limited support for this, see https://bugs.openjdk.java.net/browse/JDK-8170888 for details.

For older Java releases there is a good script from @fabric8io-images: https://github.com/fabric8io-images/java/blob/master/images/centos/openjdk8/jdk/container-limits

Also, it is not easy to calculate the amount of JVM heap and the resulting cgroup limits, as the JVM also needs memory for other things, e.g. metaspace, JIT, ...

A few words on the whole topic of JVM memory management for containers would be a very good addition. Articles like https://developers.redhat.com/blog/2017/03/14/java-inside-docker/ provide a good starting point.

arun-gupta commented 6 years ago

@PaulSandoz FYI, could not find Mikael's github handle.

sgandon commented 6 years ago

I suppose this is the link https://github.com/fabric8io-images/java/blob/84dddb5c8a72650e12c02d2056b86146edddac66/images/centos/openjdk8/jdk/container-limits But it has been replaced with a single run-java.sh script https://github.com/fabric8io-images/java/blob/7f696790875d1ff005bf98d05dae33d879d844e5/images/centos/openjdk8/jdk/run-java.sh#L162

pgordon9 commented 5 years ago

Would love to see points on how to estimate how much off-heap memory is typically needed.