<h2>TLDR;</h2>
<p>Why bother settings <code>-Xmx</code> and <code>-Xms</code> in a kubernetes app?</p>
<hr />
<p>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 <code>JAVA_OPTS -Xmx</code> to 800mb.</p>
<p><a href="https://i.stack.imgur.com/szv9K.png" rel="noreferrer"><img src="https://i.stack.imgur.com/szv9K.png" alt="enter image description here" /></a></p>
<p>I've found lots of good material on best settings for <code>-Xmx</code> (eg <a href="https://akobor.me/posts/heap-size-and-resource-limits-in-kubernetes-for-jvm-applications" rel="noreferrer">this one</a>), but can't find a straight answer to the following question: do we really need to set <code>-Xmx</code> (and less importantly, <code>-Xms</code>) in a kubernetes container?  We've already set hard limits on the container
<code>resources</code>, 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 <code>-Xmx</code> in proportion to kubernets containers?</p>

Adam Hughes
<p>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.</p>
<p>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.</p>
<p>Note that hitting the memory limit of the container will lead to killing the process and restarting the container.</p>
<p>I suggest you use a sane value, as determined from operational testing, for Xmx and Xms to get a stable memory usage.</p>
