Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we set -Xmx (max java heap size) in a kubernetes container

TLDR;

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.

enter image description here

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?

like image 730
Adam Hughes Avatar asked Dec 09 '25 17:12

Adam Hughes


1 Answers

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/

like image 125
Ondrej Bozek Avatar answered Dec 11 '25 06:12

Ondrej Bozek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!