TheJediCowboy
TheJediCowboy

Reputation: 9222

Docker OpenJDK -Xmx or

I am trying to get a service running in a container, but ran into the OutOfMemoryError: Java heap space when the service in the container starts up (it does some memory intensive stuff).

I am trying to add the options for -Xmx1024 -Xms1024, but that didn't work.

I also attempted to use

-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap

as the OpenJDK Docker Hub documentation mentions, however when I run that, it fails with the following error:

Unrecognized VM option 'UnlockExperimentalVMOptions XX:+UseCGroupMemoryLimitForHeap'

The current Dockerfile that I am using looks like the following

FROM openjdk:8u131

...

...

CMD ["java","-XshowSettings:vm -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap","-jar", "target/my-jar.jar"]

Upvotes: 3

Views: 5691

Answers (2)

the8472
the8472

Reputation: 43042

OpenJDK 10 and up detect the cgroup limit automatically without any additional flags (JDK-8146115). In addition they also let you specify the maximum heap size as fraction of that limit via the MaxRAMPercentage option (JDK-8186248).

Upvotes: 4

Stephen C
Stephen C

Reputation: 718698

I think you are specifying the arguments incorrectly. This:

CMD ["java",
     "-XshowSettings:vm -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap",
     "-jar", "target/my-jar.jar"]

should be:

CMD ["java",
     "-XshowSettings:vm",
     "-XX:+UnlockExperimentalVMOptions",
     "-XX:+UseCGroupMemoryLimitForHeap",
     "-jar", "target/my-jar.jar"]

When you use CMD [ ... ] you need to provide the arguments exactly as the shell would provide them. This could also be why you couldn't get -Xmx1024 -Xms1024 to work.

Upvotes: 2

Related Questions