<p>I am trying to size our pods using the actuator metrics info. With the below K8 resource quota configuration;</p>

<pre><code>resources:
 requests:
  memory: "512Mi"
 limits:
  memory: "512Mi"
</code></pre>

<p>We are observing that <code>jvm.memory.max</code> returns ~1455 mb. I understand that this value includes heap and non-heap. Further drilling into the api (<code>jvm.memory.max?tag=area:nonheap</code>) and (<code>jvm.memory.max?tag=area:heap</code>) results in ~1325mb and ~129mb respectively.</p>

<p>Obviously with the non-heap set to max out at a value greater than the K8 limit, the container is bound to get killed eventually. But why is the jvm (non-heap memory) not bounded by the memory configuration of the container (configured in K8)?</p>

<p>The above observations are valid with java 8 and java 11. The below blog discusses the experimental options with java 8 where CPU and heap configurations are discussed but no mention of non-heap. What are some suggestions to consider in sizing the pods?</p>

<p><code>-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap</code>
<a href="https://blogs.oracle.com/java-platform-group/java-se-support-for-docker-cpu-and-memory-limits" rel="nofollow noreferrer">Source</a></p>

<p>Java 8 has a few flags that can help the runtime operate in a more container aware manner:</p>

<pre><code>java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -jar app.jar
</code></pre>

<p>Why you get maximum JVM heap memory of 129 MB if you set the maximum container memory limit to 512 MB? So the answer is that memory consumption in JVM includes both heap and non-heap memory. The memory required for class metadata, JIT complied code, thread stacks, GC, and other processes is taken from the non-heap memory. Therefore, based on the cgroup resource restrictions, the JVM reserves a portion of the memory for non-heap use to ensure system stability.
The exact amount of non-heap memory can vary widely, but a safe bet if you’re doing resource planning is that the heap is about 80% of the JVM’s total memory. So if you set the set maximum heap to 1000 MB, you can expect that the whole JVM might need around 1250 MB.</p>

<p>The JVM read that the container is limited to 512M and created a JVM with maximum heap size of ~129MB. Exactly 1/4 of the container memory as defined in the JDK ergonomic page.</p>

<p>If you dig into the <a href="https://docs.oracle.com/javase/9/gctuning/parallel-collector1.htm#JSGCT-GUID-74BE3BC9-C7ED-4AF8-A202-793255C864C4" rel="nofollow noreferrer">JVM Tuning guide</a> you will see the following.</p>

<blockquote>
  <p>Unless the initial and maximum heap sizes are specified on the command line, they're calculated based on the amount of memory on the machine. The default maximum heap size is one-fourth of the physical memory while the initial heap size is 1/64th of physical memory. The maximum amount of space allocated to the young generation is one third of the total heap size.</p>
</blockquote>

<p>You can find more information about it <a href="https://developers.redhat.com/blog/2017/03/14/java-inside-docker/" rel="nofollow noreferrer">here</a>.</p>
