Home > Back-end >  JAVA_OPTS ineffective to set the -Xmx in a Java Docker image?
JAVA_OPTS ineffective to set the -Xmx in a Java Docker image?

Time:01-03

my understanding is that the maximum amount of Java memory in a Docker container depends on the Docker constraints and the JVM settings. However, the only change I can see in the Max Heap Size depends on the docker --memory parameter. For example, here I'm starting a Java program (openjdk17) that prints the Max memory settings:

docker run -it -e JAVA_OPTS="-Xmx1g" --memory 2g javatest
Max Heap Size = maxMemory() = 536870912

Same, changing the JAVA_OPTS:

docker run -it -e JAVA_OPTS="-Xmx64mb" --memory 2g javatest
Max Heap Size = maxMemory() = 536870912

The Dockerfile:

FROM openjdk:17
COPY ./Example.class /tmp
WORKDIR /tmp
ENTRYPOINT ["java","Example"]

Is there any other env var that I can use to set the Max memory ?

CodePudding user response:

I think the only way to make it work is to rewrite your ENTRYPOINT to include the JAVA_OPTS env variable. For example:

FROM openjdk:17
COPY ./Example.class /tmp
WORKDIR /tmp
ENTRYPOINT exec java $JAVA_OPTS Example
  • Related