javadockerjava-12

JDK12 memory behavior in docker container


It was my understanding that as of JDK12 the old experimental flag -XX:+UseCGroupMemoryLimitForHeap was removed but going to be baseline e.g. all JDK12 apps would read cgroup memory and set that as -Xms and -Xmx internally by default. However when I ran -XX:+PrintFlagsFinal with no -Xmx specified it showed I had only 512MB of memory for MaxHeap in a container with a limit of 2GB of memory allocated.

Did I miss something and the community decided to remove the -XX:+UseCGroupMemoryLimitForHeap flag with no intention of supporting behavior?


Solution

  • This is correct. Java 12 respects cgroup RAM limit (2GB) and ignores the total RAM available on the host (which is larger than 2GB). Since -Xmx wasn't specified the default JVM behavior is to use 25% of available RAM and you end up with 512 MB heap. In practice -Xmx is a specific value which overrides -XX:+UseCGroupMemoryLimitForHeap and other flags governing dynamic heap sizing.

    Assuming you don't want to specify -Xmx directly you most likely want to use -XX:MaxRAMFraction=1 which will make the JVM use 100% of the available RAM. Do note that under load it may kill the container.