Reputation: 16299
Why bother settings -Xmx
and -Xms
in a kubernetes app?
We've inherited a kubernetes app that is having out of memory errors. It looks like the initial devs set kubernetes resource limits (.95 CPU core and 4Gb memory) as shown below. However, they also set the max heap size in JAVA_OPTS -Xmx
to 800mb.
I've found lots of good material on best settings for -Xmx
(eg this one), but can't find a straight answer to the following question: do we really need to set -Xmx
(and less importantly, -Xms
) in a kubernetes container? We've already set hard limits on the container
resources
, so what is the point to setting these flags? If we removed them altogether, what would the consequence be? Would the app GC more often? Would it scale heap size dynamically or would heap size max be fixed at some default maxium like 256MB? Is there any rule of thumb of setting -Xmx
in proportion to kubernets containers?
Upvotes: 14
Views: 17615
Reputation: 25
Use Xms and Xmx to set your heap min and max. Do not use MaxRAMPercentage because when Java no longer works (as when Kubernetes moved to cgroups v2), the JVM will default to some values are incorrect and will lead to failures.
Source: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8230305
Upvotes: -2
Reputation: 11481
Since Java 10 and Java 8u191 there should be quite good support for Java determining actual memory available in the container. It's not necessary to use -Xmx
anymore. It's recommended to use XX:MaxRAMPercentage
with a value ranging between 75.0 and 80.0 (e.g. XX:MaxRAMPercentage=75.0
)
Sources:
https://www.atamanroman.dev/development/2019/09/11/usecontainersupport-to-the-rescue.html
https://pretius.com/blog/jvm-kubernetes/
Upvotes: 4
Reputation: 12009
If you don't set a maximum heap size the behavior depends on the java version used. Current JREs support determining the container limits and use that to guide their internal heuristics.
when running with an older version the memory to be used for the heap will be determined based on the physical memory visible to the container, which might be excessive.
Note that hitting the memory limit of the container will lead to killing the process and restarting the container.
I suggest you use a sane value, as determined from operational testing, for Xmx and Xms to get a stable memory usage.
Upvotes: 4