0

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 ?

Carla
  • 3,064
  • 8
  • 36
  • 65
  • 1
    `JAVA_OPTS` isn't considered by the `java` executable directly. Many third-party applications (and application servers) use this variable as a common place to put additional command line arguments for the `java` executable, but interpreting (i.e. passing it on) is up to the caller of `javac`. See [this question](https://stackoverflow.com/questions/5241743/how-do-i-use-the-java-opts-environment-variable). – Joachim Sauer Jan 02 '23 at 15:15
  • Does this answer your question? [Difference between \_JAVA\_OPTIONS, JAVA\_TOOL\_OPTIONS and JAVA\_OPTS](https://stackoverflow.com/questions/28327620/difference-between-java-options-java-tool-options-and-java-opts) – Andrey B. Panfilov Jan 02 '23 at 15:36

1 Answers1

1

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
Francesco Marchioni
  • 4,091
  • 1
  • 25
  • 40