<p>In a container-based environment such as Kubernetes, the UseContainerSupport JVM feature is handy as it allows configuring heap size as a percentage of container memory via options such as XX:MaxRAMPercentage instead of a static value via Xmx. This way you don't have to potentially adjust your JVM options every time the container memory limit changes, potentially allowing use of vertical autoscaling. The primary goal is hitting a Java OufOfMemoryError rather than running out of memory at the container (e.g. K8s OOMKilled).</p>
<p>That covers heap memory. In applications that use a significant amount of direct memory via NIO (e.g. gRPC/Netty), what are the options for this? The main option I could find is XX:MaxDirectMemorySize, but this takes in a static value similar to Xmx.</p>

<p>There's no similar switch for MaxDirectMemorySize as far as I know.
But by default (if you don't specify <code>-XX:MaxDirectMemorySize</code>) the limit is same as for <code>MaxHeapSize</code>.
That means, if you set <code>-XX:MaxRAMPercentage</code> then the same limit applies to <code>MaxDirectMemory</code>.</p>
<p>Note: that you cannot verify this simply via <code>-XX:+PrintFlagsFinal</code> because that prints 0:</p>
<pre><code>java -XX:MaxRAMPercentage=1 -XX:+PrintFlagsFinal  -version  | grep 'Max.*Size'
...
 uint64_t MaxDirectMemorySize                      = 0                                         {product} {default}
   size_t MaxHeapSize                              = 343932928                                 {product} {ergonomic}
...
openjdk version &quot;17.0.2&quot; 2022-01-18
...
</code></pre>
<p>See also <a href="https://dzone.com/articles/default-hotspot-maximum-direct-memory-size" rel="nofollow noreferrer">https://dzone.com/articles/default-hotspot-maximum-direct-memory-size</a> and <a href="https://stackoverflow.com/questions/53543062/replace-access-to-sun-misc-vm-for-jdk-11">Replace access to sun.misc.VM for JDK 11</a></p>
<p>My own experiments here: <a href="https://github.com/jumarko/clojure-experiments/pull/32" rel="nofollow noreferrer">https://github.com/jumarko/clojure-experiments/pull/32</a></p>
