<p>I'm running a containerized java application in Kubernetes.</p>

<p>In order to make the jvm reserve memory according to the container specifications, the flags <code>-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap</code> must be set.</p>

<p>If both those flags are set along with Xms and Xmx flags, what would the behavior of the jvm be? Do one flag overwrite the other?  </p>

<p>For example, if we had <code>java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms -Xms2500M -Xmx2500M -jar myjar.jar</code>
in a pod with container limits 4Gi for requests and 4Gi for responses, in a host machine that has 128Gi memory, how much memory would the JVM reserve?</p>

<p>The -Xmx flag overwrites the -XX:+UseCGroupMemoryLimitForHeap flag.</p>

<p>The flag -XX:+UseCGroupMemoryLimitForHeap lets the JVM detect what the max heap size in a container should be.</p>

<p>The -Xmx flag set the max heap size to a fixed size.</p>

<p>To answer your example, the JVM would reserve 2500M heap space. There will be some additional memory usage for non-heap and jvm stuff.</p>

<p>To further tune your memory usage in a container you could use the -XX:MaxRAMFraction flag. 
See this article: <a href="https://blog.csanchez.org/2017/05/31/running-a-jvm-in-a-container-without-getting-killed/" rel="noreferrer">https://blog.csanchez.org/2017/05/31/running-a-jvm-in-a-container-without-getting-killed/</a></p>
