I faced an interesting problem trying to analyze a memory consumption in my Java application running on docker container vs host machine.
You might want to try and measure again, with an openJDK 8u212 or more (April, 16th 2019). (no Oracle JDK, since their license has changed)
See "Docker support in Java 8 — finally!" from Grzegorz Kocur.
Now:
There is no need to use any hacky workarounds in a docker entrypoint, nor setting Xmx as fixed value anymore.
Docker support was also backported to Java 8.
Let’s check the newest openjdk image tagged as 8u212. We’ll limit the memory to 1G and use 1 CPU:docker run -ti --cpus 1 -m 1G openjdk:8u212-jdk
You can fine-tune the heap-size with new flags (already present in Java 10+, but now back ported to Java 8), and explained here.
-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage
If for some reason the new JVM behaviour is not desired it can be switched off using -
XX:-UseContainerSupport
.