How to set Java heap size (Xms/Xmx) inside Docker container?
Asked Answered
N

8

60

As of raising this question, Docker looks to be new enough to not have answers to this question on the net. The only place I found is this article in which the author is saying it is hard, and that's it.

Negotiation answered 28/4, 2015 at 15:25 Comment(0)
Q
50

I agree that it depends on what container you're using. If you are using the official Tomcat image, it looks like it's simple enough, you will need to pass the JAVA_OPTS environment variable with your heap settings:

docker run --rm -e JAVA_OPTS='-Xmx1g' tomcat

See How to set JVM parameters?

Quirites answered 28/4, 2015 at 18:14 Comment(0)
V
58

Note that in a docker-compose.yml file - you'll need to leave out the double-quotes:

  environment:
  - JVM_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m

or

  environment:
  - CATALINA_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m
Vc answered 25/8, 2016 at 5:25 Comment(3)
setting CATALINA_OPTS is a bad idea, it can blow away a lot of other options set by default in the containerCalamitous
1 year later i actually realized that JVM_OPTS does nothing for my project (got OOM) :) what really works is _JAVA_OPTIONSGrange
JVM_OPTS didn't work for me either but _JAVA_OPTIONS did. Thanks both!Wilson
Q
50

I agree that it depends on what container you're using. If you are using the official Tomcat image, it looks like it's simple enough, you will need to pass the JAVA_OPTS environment variable with your heap settings:

docker run --rm -e JAVA_OPTS='-Xmx1g' tomcat

See How to set JVM parameters?

Quirites answered 28/4, 2015 at 18:14 Comment(0)
C
43

Update: Regarding this discussion, Java has upped there game regarding container support. Nowadays (or since JVM version 10 to be more exact), the JVM is smart enough to figure out whether it is running in a container, and if yes, how much memory it is limited to.

So, rather than setting fixed limits when starting your JVM, which you then have to change in line with changes to your container limits (resource limits in the K8s world), simply do nothing and let the JVM work out limits for itself.

Without any extra configuration, the JVM will set the maximum heap size to 25% of the allocated memory. Since this is frugal, you might want to ramp that up a bit by setting the -XX:MaxRAMPercentage attribute. Also, there is -XX:InitialRAMPercentage for initial heap size and -XX:MinRAMPercentage for containers with less than 96MB RAM.

For more information on the topic, here is an excellent overview.

Chlorophyll answered 16/12, 2020 at 16:59 Comment(4)
This is really great infoRiparian
Please keep in mind that this mechanism was broken again with cgroups v2 and was fixed with JVM version 15: bugs.openjdk.org/browse/JDK-8230305Chlorophyll
Thank you sir. you saved my time again. It did not work for me. With 17, it works just great. Please answer this info as part of this question. So that i could accept and you could help others. #72596903Riparian
You are welcome - answered your question thread.Chlorophyll
S
21

You can also just place those settings in your image so something like the following would exist in your Dockerfile:

ENV JAVA_OPTS="-XX:PermSize=1024m -XX:MaxPermSize=512m"
Spermiogenesis answered 21/3, 2016 at 18:51 Comment(1)
I'm pretty sure that setting a JAVA_OPTS environment variable will only work in the official Tomcat image. It may not work if you've created your own Tomcat image (eg by using Ubuntu base and installing Tomcat as part of build)Laplace
U
9

you can do it by specifying java options environment in docker compose file

env: 
  - name: _JAVA_OPTIONS
    value: "-Xmx1g"

it will change the heap size.

Udella answered 3/2, 2020 at 13:30 Comment(1)
Do you maybe know why some environments use _JAVA_OPTIONS? This is the one that worked for me, while JAVA_OPTS or JVM_OPTS didn't work.Chrono
S
6

It all depends how your Java application is packaged and how it's configuration files are exposed using Docker.

For example the official tomcat image states that the configuration file is available in the default location: /usr/local/tomcat/conf/

So easy to override entire directory or just one configuration file:

docker run -it --rm -p 8080:8080 -v $PWD/catalina.properties:/usr/local/tomcat/conf/catalina.properties tomcat:8.0
Skycap answered 28/4, 2015 at 18:1 Comment(0)
G
3

You can set the Xms and Xmx values in the dockerfile using the following way as well.

ENTRYPOINT ["java", "-Xms128M ", "-Xmx256M", "-jar", "your-precious-service-1.0.0.jar"]

You can also ignore the Xms value and just set the Xmx value.

ENTRYPOINT ["java", "-Xmx256M", "-jar", "your-precious-service-1.0.0.jar"] 

For more info please read : https://akobor.me/posts/heap-size-and-resource-limits-in-kubernetes-for-jvm-applications

Goldin answered 17/3, 2023 at 4:58 Comment(0)
I
0

I am little late on the question, I will use @Fritz Duchardt answer.

But if someone is struggling and getting "Invalid initial heap size" , then we should have two separate args for -Xms ans -Xmx as below :

"java","-Xms256m","-Xmx1G","-Dspring.profiles.active=${ENV}","-jar","app-0.0.1-SNAPSHOT.jar"
Infective answered 20/2 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.