<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body dir="auto">
<div dir="ltr"></div>
<div dir="ltr">Okay it seems to me that the use case you are describing is wanting a container with an enforced memory limit. It should quack like a cgroup and walk like a cgroup but must not actually use cgroups for some reason.</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">Cgroups were seemingly built for this use case and has a complete view of the memory usage in the container due to being an OS feature.</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">Conversely, if the custom ad-hoc container environment does not have OS support for the memory limit, then the app can temporarily exceed the memory limit, and hence won’t be as effective of a limit.</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">But if you want to actually enforce a memory limit such that the app dies if it exceeds the limit I can’t help but wonder… why not use a cgroup to declare that limit though?</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">Regardless, I wonder if what you actually want for your use case is a way to tell AHS what the max memory of the entire JVM should be, similar to the -XX:RssLimit Thomas Stuefe proposed: <a href="https://bugs.openjdk.org/browse/JDK-8321266">https://bugs.openjdk.org/browse/JDK-8321266</a></div>
<div dir="ltr"><br>
</div>
<div dir="ltr">In other words, letting the JVM know that it has a bound on memory, and have AHS know about and try to adapt the heap such that the JVM memory usage is below the limit when native memory goes up and down. In other words, let the heap heuristics
 live in the JVM. Perhaps then the limit would also be static, or do the containers themselves actually grow and shrink at runtime, or was the dynamic nature of CurrentMaxHeapSize mostly an artifact of out sourcing the heap heuristics of an otherwise static
 custom container limit?</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">/Erik</div>
<div dir="ltr"><br>
<blockquote type="cite">On 11 Apr 2025, at 00:15, Man Cao <manc@google.com> wrote:<br>
<br>
</blockquote>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div>> If the custom container app allocates 300 GB native memory with, for example, panama APIs or JNI, what will happen? Is it allowed, or limited?</div>
<div><br>
</div>
<div>I suppose the more accurate way to put it is "if an app inside the custom container environment allocates 300 GB native memory ...".  The custom container environment itself is not a Java app.</div>
<div><br>
</div>
<div>If the container memory limit is 310GiB, container usage is 305GiB, and the app's current Java heap size is 3GiB, and Xmx is 20GiB, then the app could set CurrentMaxHeapSize=8G  (310 - 305 + 3), or CurrentMaxHeapSize=7G (to give 1GiB head room for growth
 from other non-heap memory: code cache, thread stack, metaspace, etc.), to prevent running out of container memory limit. Note that the app should actively monitor container usage to adjust CurrentMaxHeapSize, e.g. increasing CurrentMaxHeapSize when container
 usage drops. If the app keeps allocating more native memory, CurrentMaxHeapSize will further drop, and it will eventually die with Java OutOfMemoryError.</div>
<div><br>
</div>
<div>In the above case, the JVM is unaware of the 310G container limit or the 305G container usage.</div>
<div><br>
</div>
<div>
<div dir="ltr" class="gmail_signature">
<div dir="ltr">-Man</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</body>
</html>