27

What does this option do in docker file?

ENTRYPOINT java -XX:+UseContainerSupport $JAVA_OPTIONS -jar /myapp.jar

Will the docker container start without this parameter? I checked one article which says

enable memory support

but it is still not clear to me.

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114

2 Answers2

35

Starting from Java 10, this parameter (which is enabled by default) is used to make the JVM take the container memory limits into account when allocating the heap size, not the host machine configuration.

This option was backported to Java 8: https://www.oracle.com/technetwork/java/javase/8u191-relnotes-5032181.html

Examples:

If you run:

docker run **-m 1gb** openjdk:8u131 java -XshowSettings:vm -version

The result is going to be (on my machine Ubuntu with 8gb)

Max. Heap Size (Estimated): 1.68G

I set a memory limit for the container but it ignored and used the host config (it uses by default total memory/4)

Now if I run the version that has the new feature (link above) you can see that the container memory limite was taken into account:

docker run **-m 1g** openjdk:8u191-jre-alpine java -XshowSettings:vm -version

Result (total memory / 4):

VM settings:
    Max. Heap Size (Estimated): 247.50M
    Ergonomics Machine Class: server
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "1.8.0_191"

At the time I'm writing this the LATEST version of the openjdk:8 image is 222 so you can use this version. That has the feature included.

For more information:

Explains this flag use in Java 10: https://medium.com/adorsys/jvm-memory-settings-in-a-container-environment-64b0840e1d9e

Using this flag with Java 8: https://blog.softwaremill.com/docker-support-in-new-java-8-finally-fd595df0ca54

roundcrisis
  • 17,276
  • 14
  • 60
  • 92
Thiago
  • 864
  • 1
  • 9
  • 16
19

Yes. The container will start without -XX:+UseContainerSupport.

-XX:+UseContainerSupport is used to allocate a larger fraction of memory.

To prevent the JVM adjusting the maximum heap size when running in a container, set -XX:-UseContainerSupport.

In addition to that, https://www.eclipse.org/openj9/docs/xxusecontainersupport/ might be helpful.

aboger
  • 2,214
  • 6
  • 33
  • 47
ionenorch
  • 208
  • 2
  • 6
  • 18
    As I read -XX:+UseContainerSupport is turned on by default so even without it on JDK 10 and newer it will be the same as having that. Only useful is option to disable it with -XX:-UseContainerSupport. But in that case JVM will not be able to observe the limits set by docker container, it will read directly limits of whole container host so instead of getting java.lang.OutOfMemoryError: Java heap space the process would be brutally killed when it hits container limit. No application logs would show the problem... So in a word do not disable this option – aurelije Jul 26 '19 at 10:09
  • Is this parameter enabled by default on java 8? – Toofy Dec 02 '20 at 14:25
  • From the release notes they say it's enabled by default on Java8 as well: https://www.oracle.com/java/technologies/javase/8u191-relnotes.html – cipher0 Sep 23 '21 at 11:20