<p>In a <code>Kubernetes</code> cluster with numerous microservices, one of them is used exclusively for a <code>Java Virtual Machine</code> (JVM) that runs a <code>Java 1.8</code> data processing application.</p>
<p>Up to recently, jobs running in that JVM pod consumed less than 1 GB of RAM, so the pod has been setup with 4 GB of maximum memory, without any explicit heap size settings for the JVM.</p>
<p>Some new data now require about 2.5 GB for the entire pod, including the JVM (as reported by the <code>kubernetes top</code> command, after launching with an increased memory limit of 8 GB), but the pod crashes soon after starting with a limit of 4 GB.</p>
<p>Using a head size range like <code>-Xms256m -Xmx3072m</code> with a limit of 4 GB does not solve the problem. In fact, now the pod does not even start.</p>
<p>Is there any way to parameterize the JVM for accommodating the 2.5 GB needed, without increasing the 4 GB maximum memory for the pod?</p>

<p>The default &quot;max heap&quot; if you don't specify <code>-Xmx</code> is 1/4 (25%) of the host RAM.
JDK 10 improved support for containers in that it uses container's RAM limits instead of underlying host. As pointed by @David Maze this has been backported to  JDK 8.</p>
<p>Assuming you have a  sufficiently recent version of JDK 8, you can use <code>-XX:MaxRAMPercentage</code> to modify the default percentage of total RAM used for Max heap. So instead of specifying <code>-Xmx</code> you can tell, e.g. <code>-XX:MaxRAMPercentage=75.0</code>. See also <a href="https://blog.arkey.fr/2020/10/27/maxrampercentage-is-not-what-i-wished-for/" rel="nofollow noreferrer">https://blog.arkey.fr/2020/10/27/maxrampercentage-is-not-what-i-wished-for/</a></p>
<p>Here's an example using alpine JDK docker image: <a href="https://hub.docker.com/_/openjdk" rel="nofollow noreferrer">https://hub.docker.com/_/openjdk</a> (see section &quot;Make JVM respect CPU and RAM limits&quot; in particular).</p>
<pre><code># this is running on the host with 2 GB RAM
docker run --mount type=bind,source=&quot;$(pwd)&quot;,target=/pwd -it openjdk:8

# running with MaxRAMPercentage=50 =&gt; half of the available RAM is used as &quot;max heap&quot;
root@c9b0b4d9e85b:/# java -XX:+PrintFlagsFinal -XX:MaxRAMPercentage=50.0 -version | grep -i maxheap
    uintx MaxHeapFreeRatio                          = 100                                 {manageable}
    uintx MaxHeapSize                              := 1044381696                          {product}
openjdk version &quot;1.8.0_265&quot;
OpenJDK Runtime Environment (build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

# running without MaxRAMPercentage =&gt; default 25% of RAM is used
root@c9b0b4d9e85b:/# java -XX:+PrintFlagsFinal -version | grep -i maxheap
    uintx MaxHeapFreeRatio                          = 100                                 {manageable}
    uintx MaxHeapSize                              := 522190848                           {product}
openjdk version &quot;1.8.0_265&quot;
</code></pre>
