<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kkkevin</title>
    <description>The latest articles on DEV Community by kkkevin (@kkkevins).</description>
    <link>https://dev.to/kkkevins</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4216369%2F024f4b18-a778-4a0d-acbe-36d24ed93d1e.png</url>
      <title>DEV Community: kkkevin</title>
      <link>https://dev.to/kkkevins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kkkevins"/>
    <language>en</language>
    <item>
      <title>MaxRAMPercentage vs Xmx: Sizing Java Heap in Containers</title>
      <dc:creator>kkkevin</dc:creator>
      <pubDate>Fri, 13 Nov 2026 02:30:03 +0000</pubDate>
      <link>https://dev.to/kkkevins/maxrampercentage-vs-xmx-sizing-java-heap-in-containers-1ook</link>
      <guid>https://dev.to/kkkevins/maxrampercentage-vs-xmx-sizing-java-heap-in-containers-1ook</guid>
      <description>&lt;p&gt;Your Java service runs in a Kubernetes pod with a memory limit, and you have to decide how big the heap may grow. There are two ways to say it: a fixed &lt;code&gt;-Xmx&lt;/code&gt;, or a percentage with &lt;code&gt;-XX:MaxRAMPercentage&lt;/code&gt;. This guide explains how &lt;strong&gt;MaxRAMPercentage vs Xmx&lt;/strong&gt; behaves inside a container, which one to pick, and how to choose the number so the pod is not OOMKilled. It is written for engineers who deploy JVM services on Kubernetes and have to defend the setting in a review.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fynz6zgccb3bkq5z4yyia.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fynz6zgccb3bkq5z4yyia.jpg" alt="Stacks of white refrigerated shipping containers at the Port of Rotterdam" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“&lt;a href="https://commons.wikimedia.org/wiki/File:Shipping_container_stacks_-_Port_of_Rotterdam_-_2014_-_2.jpg" rel="noopener noreferrer"&gt;Shipping container stacks – Port of Rotterdam – 2014 – 2&lt;/a&gt;” by AgainErick, &lt;a href="https://creativecommons.org/licenses/by-sa/4.0" rel="noopener noreferrer"&gt;CC BY-SA 4.0&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What the JVM does when you set nothing
&lt;/h2&gt;

&lt;p&gt;Since container support is on by default, the JVM reads the cgroup memory limit instead of the node’s RAM. The &lt;a href="https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html" rel="noopener noreferrer"&gt;java command reference&lt;/a&gt; says the default for &lt;code&gt;-XX:+UseContainerSupport&lt;/code&gt; is true, and that the VM uses the detected memory and processor count to allocate resources.&lt;/p&gt;

&lt;p&gt;The same reference lists these heap defaults, all measured against the memory the JVM considers available (capped by &lt;code&gt;-XX:MaxRAM&lt;/code&gt;, which defaults to the available memory or 128 GB, whichever is lower):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-XX:MaxRAMPercentage&lt;/code&gt;: 25 percent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-XX:InitialRAMPercentage&lt;/code&gt;: 1.5625 percent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-XX:MinRAMPercentage&lt;/code&gt;: 50 percent, applied to small heaps of roughly 125 MB.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Microsoft’s &lt;a href="https://learn.microsoft.com/en-us/azure/developer/java/containers/overview" rel="noopener noreferrer"&gt;guide to containerizing Java applications&lt;/a&gt; turns that into a table of default maximum heap by available memory: up to 256 MB gives 50 percent, 256 MB to 512 MB gives about 127 MB, and above 512 MB gives 25 percent. The practical result is that a 2 GiB container with no flags gets a heap of only about 512 MiB. That is safe but wasteful, which is why most teams change it.&lt;/p&gt;

&lt;p&gt;Watch the naming trap: despite its name, &lt;code&gt;-XX:MinRAMPercentage&lt;/code&gt; does not set a minimum heap. As the Microsoft guide points out, it sets the maximum percentage for systems with up to 256 MB of RAM. To set a starting heap, use &lt;code&gt;-Xms&lt;/code&gt; or &lt;code&gt;-XX:InitialRAMPercentage&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  MaxRAMPercentage vs Xmx: how they differ
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;-Xmx&lt;/code&gt; is an absolute size and is equivalent to &lt;code&gt;-XX:MaxHeapSize&lt;/code&gt; according to the same reference. &lt;code&gt;-XX:MaxRAMPercentage&lt;/code&gt; is relative: the JVM multiplies the percentage by the memory it detected at startup. Everything else follows from that.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;-Xmx&lt;/th&gt;
&lt;th&gt;-XX:MaxRAMPercentage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Value depends on the pod limit&lt;/td&gt;
&lt;td&gt;No, you must keep it in sync by hand&lt;/td&gt;
&lt;td&gt;Yes, it follows the limit automatically&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same manifest for several sizes&lt;/td&gt;
&lt;td&gt;Needs per-size config&lt;/td&gt;
&lt;td&gt;Works unchanged&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Predictable exact heap&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Only after you do the arithmetic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Risk if the limit is lowered later&lt;/td&gt;
&lt;td&gt;Heap may now exceed the limit, so the pod is OOMKilled&lt;/td&gt;
&lt;td&gt;Heap shrinks with the limit&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If both are set, the explicit &lt;code&gt;-Xmx&lt;/code&gt; is what ends up as the maximum heap. Do not rely on memory for this; check it on your JDK, as shown later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the heap cannot equal the container limit
&lt;/h2&gt;

&lt;p&gt;Kubernetes enforces memory limits reactively: the &lt;a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/" rel="noopener noreferrer"&gt;resource management documentation&lt;/a&gt; says the kernel enforces them through cgroups, and a container that goes over can be OOM-killed. The JVM uses much more than the heap: metaspace (unlimited by default, per the java reference for &lt;code&gt;-XX:MaxMetaspaceSize&lt;/code&gt;), thread stacks, the code cache, direct buffers and GC structures. The Microsoft guide states it plainly: do not set the maximum heap equal to the container memory, because that can cause container OOM errors and crashes. It suggests 75 percent of container memory for the heap as a starting point.&lt;/p&gt;

&lt;p&gt;To see where the rest goes in your own service, use the memory-pool method from &lt;a href="https://opshubs.space/java-oomkilled-in-kubernetes-find-the-memory-pool-with-nmt/" rel="noopener noreferrer"&gt;Java OOMKilled in Kubernetes: Find the Memory Pool with NMT&lt;/a&gt;. For a quick estimate, the &lt;a href="https://opshubs.space/tools/jvm-container-memory-calculator/" rel="noopener noreferrer"&gt;JVM Container Memory Calculator&lt;/a&gt; adds heap, metaspace and direct memory for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the percentage: a worked example
&lt;/h2&gt;

&lt;p&gt;The numbers below are hypothetical and computed from the limit alone. The “left for non-heap” column is limit minus heap.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Memory limit&lt;/th&gt;
&lt;th&gt;25% (default)&lt;/th&gt;
&lt;th&gt;50%&lt;/th&gt;
&lt;th&gt;75%&lt;/th&gt;
&lt;th&gt;Left at 75%&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;512Mi&lt;/td&gt;
&lt;td&gt;128Mi&lt;/td&gt;
&lt;td&gt;256Mi&lt;/td&gt;
&lt;td&gt;384Mi&lt;/td&gt;
&lt;td&gt;128Mi&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1024Mi&lt;/td&gt;
&lt;td&gt;256Mi&lt;/td&gt;
&lt;td&gt;512Mi&lt;/td&gt;
&lt;td&gt;768Mi&lt;/td&gt;
&lt;td&gt;256Mi&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2048Mi&lt;/td&gt;
&lt;td&gt;512Mi&lt;/td&gt;
&lt;td&gt;1024Mi&lt;/td&gt;
&lt;td&gt;1536Mi&lt;/td&gt;
&lt;td&gt;512Mi&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4096Mi&lt;/td&gt;
&lt;td&gt;1024Mi&lt;/td&gt;
&lt;td&gt;2048Mi&lt;/td&gt;
&lt;td&gt;3072Mi&lt;/td&gt;
&lt;td&gt;1024Mi&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A percentage hides an important fact: non-heap memory does not grow in proportion to the limit. A service needing roughly the same metaspace, stacks and code cache will have a much larger share of a 512Mi pod taken up by them than of a 4096Mi pod. So 75 percent can be fine at 4096Mi and too high at 512Mi. A better way to choose is to work backwards from a measurement:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Measure non-heap memory under load (NMT, see the link above). Suppose, hypothetically, it is 280Mi.&lt;/li&gt;
&lt;li&gt;Add a safety margin for spikes, for example 100Mi. Non-heap budget: 380Mi.&lt;/li&gt;
&lt;li&gt;Subtract from the limit: with a 1024Mi limit, the heap may use 1024 – 380 = 644Mi.&lt;/li&gt;
&lt;li&gt;Divide: 644 / 1024 = 62.9 percent. Round down to 62.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For a 512Mi limit with a measured 200Mi of non-heap plus a 64Mi margin, the result is (512 – 264) / 512 = 48.4 percent, so 48. Note that this is below the 50 percent the JVM would already use for tiny heaps, which shows why small pods need measuring rather than a blanket 75.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://opshubs.space/tools/kubernetes-qos-class-checker/" rel="noopener noreferrer"&gt;Kubernetes QoS Class Checker&lt;/a&gt; is useful at this point to confirm how your requests and limits translate: &lt;code&gt;Mi&lt;/code&gt; and &lt;code&gt;M&lt;/code&gt; are different units, and a limit written as &lt;code&gt;1G&lt;/code&gt; is 1,000,000,000 bytes, not 1024Mi.&lt;/p&gt;

&lt;h2&gt;
  
  
  A ready-to-use Deployment snippet
&lt;/h2&gt;

&lt;p&gt;The manifest below sets the limit and makes the heap follow it. Requests equal limits, so the memory is reserved, which is the condition under which Microsoft recommends setting the initial heap equal to the maximum.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: orders-api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: orders-api
  template:
    metadata:
      labels:
        app: orders-api
    spec:
      containers:
      - name: app
        image: registry.example.com/orders-api:1.0.0
        resources:
          requests:
            memory: "1024Mi"
          limits:
            memory: "1024Mi"
        env:
        - name: JAVA_TOOL_OPTIONS
          value: "-XX:MaxRAMPercentage=62 -XX:InitialRAMPercentage=62"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;requests.memory&lt;/code&gt; equal to &lt;code&gt;limits.memory&lt;/code&gt; reserves the whole amount for the container.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;JAVA_TOOL_OPTIONS&lt;/code&gt; is read by the JVM at startup, so the same image works with any command line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MaxRAMPercentage=62&lt;/code&gt; gives a heap of about 635Mi on a 1024Mi limit; &lt;code&gt;InitialRAMPercentage=62&lt;/code&gt; starts the heap at that size so the JVM does not resize it repeatedly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Verify what the JVM actually chose
&lt;/h2&gt;

&lt;p&gt;Never trust the arithmetic alone. Run the same flags under the same limit and read the result. With Docker, you can reproduce a Kubernetes limit locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --rm --memory=1g eclipse-temurin:21-jre \
  java -XX:MaxRAMPercentage=62 -XX:+PrintFlagsFinal -version \
  | grep -E 'MaxHeapSize|MaxRAMPercentage|InitialHeapSize'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;--memory=1g&lt;/code&gt; sets a 1 GiB cgroup limit, &lt;code&gt;-XX:+PrintFlagsFinal&lt;/code&gt; prints every flag after ergonomics, and &lt;code&gt;grep&lt;/code&gt; keeps the three that matter. &lt;code&gt;MaxHeapSize&lt;/code&gt; is in bytes, so divide by 1048576 to get MiB. To test the precedence rule for your JDK, add &lt;code&gt;-Xmx256m&lt;/code&gt; to the same command and compare the output.&lt;/p&gt;

&lt;p&gt;For a running pod, ask the live process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl exec deploy/orders-api -- jcmd 1 VM.flags
kubectl exec deploy/orders-api -- jcmd 1 GC.heap_info
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;VM.flags&lt;/code&gt; shows the effective &lt;code&gt;MaxHeapSize&lt;/code&gt;, and &lt;code&gt;GC.heap_info&lt;/code&gt; shows how much of it is in use. This assumes the JDK image includes &lt;code&gt;jcmd&lt;/code&gt; and the Java process is PID 1; a JRE-only or distroless image will not have the tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  When a fixed Xmx is still the better choice
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You run one tuned service and want an exact, reviewed heap size in the manifest.&lt;/li&gt;
&lt;li&gt;The same image runs with very different limits and you want the heap to stay constant while only the limit changes, for example in a load test.&lt;/li&gt;
&lt;li&gt;Your measurements show non-heap memory is stable, so subtracting a fixed amount is more accurate than a percentage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you use &lt;code&gt;-Xmx&lt;/code&gt;, put a comment next to the memory limit in the manifest and change them together. The usual failure is somebody lowering the limit during a cost review and leaving the old &lt;code&gt;-Xmx&lt;/code&gt; in place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The default maximum heap is 25 percent of the detected memory for containers above 512 MB, so an untuned 2 GiB pod uses only about 512 MiB of heap.&lt;/li&gt;
&lt;li&gt;Prefer &lt;code&gt;-XX:MaxRAMPercentage&lt;/code&gt; when the same image runs with different limits; prefer &lt;code&gt;-Xmx&lt;/code&gt; when you want one exact, reviewed value.&lt;/li&gt;
&lt;li&gt;Never set the heap equal to the container limit; 75 percent is a reasonable start for larger pods, and small pods need a measured value.&lt;/li&gt;
&lt;li&gt;Work backwards: limit minus measured non-heap memory minus a margin, divided by the limit.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-XX:MinRAMPercentage&lt;/code&gt; is not a minimum heap; use &lt;code&gt;-XX:InitialRAMPercentage&lt;/code&gt; or &lt;code&gt;-Xms&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Confirm the result with &lt;code&gt;-XX:+PrintFlagsFinal&lt;/code&gt; or &lt;code&gt;jcmd VM.flags&lt;/code&gt; instead of trusting the calculation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Use a percentage when you want the heap to follow the pod limit, use &lt;code&gt;-Xmx&lt;/code&gt; when you want a fixed number, and in both cases leave room for everything the JVM keeps outside the heap. Measure that room once, derive the percentage from it, and check the effective value on the real container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://opshubs.space/java-oomkilled-in-kubernetes-find-the-memory-pool-with-nmt/" rel="noopener noreferrer"&gt;Java OOMKilled in Kubernetes: Find the Memory Pool with NMT&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://opshubs.space/tools/jvm-container-memory-calculator/" rel="noopener noreferrer"&gt;JVM Container Memory Calculator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://opshubs.space/tools/kubernetes-qos-class-checker/" rel="noopener noreferrer"&gt;Kubernetes QoS Class Checker&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://opshubs.space/maxrampercentage-vs-xmx-java-heap-containers/" rel="noopener noreferrer"&gt;OpsHubs&lt;/a&gt;, where you can also find free &lt;a href="https://opshubs.space/tools/" rel="noopener noreferrer"&gt;ops calculators&lt;/a&gt;. This article was drafted with AI assistance and reviewed before publishing.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>java</category>
      <category>devops</category>
      <category>cloudnative</category>
    </item>
    <item>
      <title>Java OOMKilled in Kubernetes: Find the Memory Pool with NMT</title>
      <dc:creator>kkkevin</dc:creator>
      <pubDate>Fri, 06 Nov 2026 02:30:04 +0000</pubDate>
      <link>https://dev.to/kkkevins/java-oomkilled-in-kubernetes-find-the-memory-pool-with-nmt-5dpb</link>
      <guid>https://dev.to/kkkevins/java-oomkilled-in-kubernetes-find-the-memory-pool-with-nmt-5dpb</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F02jkbe4dzu9jgza1nqpb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F02jkbe4dzu9jgza1nqpb.jpg" alt="A pile of laptop and desktop RAM memory modules on a wooden table" width="799" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“&lt;a href="https://commons.wikimedia.org/wiki/File:Pile_of_Laptop_%26_Desktop_RAM_Memory_Modules.jpg" rel="noopener noreferrer"&gt;Pile of Laptop &amp;amp; Desktop RAM Memory Modules&lt;/a&gt;” by Wilbysuffolk, &lt;a href="https://creativecommons.org/licenses/by-sa/4.0" rel="noopener noreferrer"&gt;CC BY-SA 4.0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Java pod that restarts with &lt;code&gt;OOMKilled&lt;/code&gt; is a frustrating problem, because the JVM often leaves no stack trace and no &lt;code&gt;OutOfMemoryError&lt;/code&gt;. The kernel simply ends the process. This guide is for engineers who run Java services on Kubernetes and need to answer one question: &lt;strong&gt;which memory pool grew&lt;/strong&gt;? We will use Native Memory Tracking (NMT) and &lt;code&gt;jcmd&lt;/code&gt; to find out, then map the result to a fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Java OOMKilled is not a Java OutOfMemoryError
&lt;/h2&gt;

&lt;p&gt;Two different things get confused. A Java &lt;code&gt;OutOfMemoryError&lt;/code&gt; is thrown by the JVM when it cannot satisfy an allocation inside its own limits, for example when the heap is full. &lt;code&gt;OOMKilled&lt;/code&gt; means the container as a whole used more memory than its Kubernetes limit. The &lt;a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/" rel="noopener noreferrer"&gt;Kubernetes documentation&lt;/a&gt; explains that memory limits are enforced by the kernel with out-of-memory kills, and that this enforcement is reactive: a container can briefly exceed the limit, and it is killed when the kernel detects memory pressure.&lt;/p&gt;

&lt;p&gt;The container’s memory is more than the Java heap. It also includes class metadata, thread stacks, JIT-compiled code, GC data structures, direct buffers and memory from native libraries. A pod can be killed while the heap graph looks healthy. That is why heap dumps alone often do not explain it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Confirm that it really was an OOM kill
&lt;/h2&gt;

&lt;p&gt;Start by checking the termination reason. The &lt;a href="https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/" rel="noopener noreferrer"&gt;Kubernetes memory tutorial&lt;/a&gt; shows that a killed container reports &lt;code&gt;reason: OOMKilled&lt;/code&gt; and &lt;code&gt;exitCode: 137&lt;/code&gt;, which is 128 + 9 (SIGKILL).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get pod my-app-7d9f -o jsonpath='{.status.containerStatuses[0].lastState.terminated}'
kubectl describe pod my-app-7d9f | grep -A6 "Last State"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;my-app-7d9f&lt;/code&gt; with your pod name. If the reason is something else (for example an error exit from your own code), this guide does not apply.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do the budget arithmetic first
&lt;/h2&gt;

&lt;p&gt;Before measuring, add up what the JVM is allowed to use. The &lt;a href="https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html" rel="noopener noreferrer"&gt;java command reference&lt;/a&gt; gives these facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-XX:MaxRAMPercentage&lt;/code&gt; defaults to 25 percent, and the RAM it is a percentage of is limited by container constraints.&lt;/li&gt;
&lt;li&gt;The default maximum code cache is 240 MB (48 MB without tiered compilation).&lt;/li&gt;
&lt;li&gt;The default thread stack size on Linux/x64 is 1024 KB.&lt;/li&gt;
&lt;li&gt;Metaspace is not limited by default (&lt;code&gt;-XX:MaxMetaspaceSize&lt;/code&gt;), and direct buffer size is chosen automatically unless you set &lt;code&gt;-XX:MaxDirectMemorySize&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take a hypothetical pod with a 1024Mi limit whose JVM runs with &lt;code&gt;-XX:MaxRAMPercentage=75&lt;/code&gt;, so the maximum heap is 768Mi. If the service has 200 threads on Linux/x64, their stacks can reserve up to 200 x 1 MB = 200 MB. Only the stack pages a thread actually touches count toward the container, so real use is usually lower, but in the worst case heap plus stacks is 968 MB. That leaves 56 MB of the 1024Mi limit for metaspace, code cache, GC structures, direct buffers and everything else. A budget this tight can explain a kill without any leak at all.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://opshubs.space/tools/jvm-container-memory-calculator/" rel="noopener noreferrer"&gt;JVM Container Memory Calculator&lt;/a&gt; does this addition for you with your own heap, metaspace, thread and direct memory inputs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enable Native Memory Tracking
&lt;/h2&gt;

&lt;p&gt;NMT is off by default. According to the java command reference, &lt;code&gt;-XX:NativeMemoryTracking&lt;/code&gt; accepts &lt;code&gt;off&lt;/code&gt;, &lt;code&gt;summary&lt;/code&gt; and &lt;code&gt;detail&lt;/code&gt;. The &lt;a href="https://docs.oracle.com/en/java/javase/21/troubleshoot/diagnostic-tools.html" rel="noopener noreferrer"&gt;Java troubleshooting guide&lt;/a&gt; states that enabling NMT costs a 5-10 percent performance drop, so turn it on in a test or canary environment first. It cannot be switched on in a running JVM; it needs a restart.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;env:
  - name: JAVA_TOOL_OPTIONS
    value: "-XX:NativeMemoryTracking=summary"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets the flag through &lt;code&gt;JAVA_TOOL_OPTIONS&lt;/code&gt;, which the JVM reads at startup, so you do not have to edit the image. Use &lt;code&gt;detail&lt;/code&gt; only when &lt;code&gt;summary&lt;/code&gt; points to a category and you need call sites.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read the NMT summary with jcmd
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;jcmd&lt;/code&gt; inside the container. In many images the Java process is PID 1. &lt;code&gt;jcmd&lt;/code&gt; ships with the JDK, so JRE-only or distroless images may not include it; in that case use an image variant that has the JDK tools, or an ephemeral debug container with a JDK that shares the pod’s process namespace. Run &lt;code&gt;jcmd&lt;/code&gt; as the same user as the Java process, or it cannot attach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl exec my-app-7d9f -- jcmd 1 VM.native_memory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output lists the total reserved and committed memory, then one block per category. The categories documented by Oracle include Java Heap, Class, Thread, Code, GC, Compiler, Internal, Symbol and Native Memory Tracking itself. The page notes that the list may change between releases. Focus on &lt;strong&gt;committed&lt;/strong&gt; values, since committed memory is what counts against the container.&lt;/p&gt;

&lt;p&gt;One limit matters here: NMT does not track allocations made by non-JVM native code, such as a JNI library. If the NMT total is far below what the container reports, suspect native libraries rather than the JVM.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compare against a baseline
&lt;/h3&gt;

&lt;p&gt;A single snapshot rarely shows growth. Take a baseline after warm-up, wait while the problem develops, then diff.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl exec my-app-7d9f -- jcmd 1 VM.native_memory baseline
# ...wait until memory has grown...
kubectl exec my-app-7d9f -- jcmd 1 VM.native_memory summary.diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The diff prints each category with a plus or minus delta. The category with the largest positive delta is your suspect. If you need allocation sites, restart with &lt;code&gt;-XX:NativeMemoryTracking=detail&lt;/code&gt; and use &lt;code&gt;detail.diff&lt;/code&gt; the same way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Match the growing pool to a fix
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Growing category&lt;/th&gt;
&lt;th&gt;Typical cause&lt;/th&gt;
&lt;th&gt;What to try&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Java Heap&lt;/td&gt;
&lt;td&gt;Heap max is too large for the limit, or real object growth&lt;/td&gt;
&lt;td&gt;Lower &lt;code&gt;-XX:MaxRAMPercentage&lt;/code&gt;, or take a heap dump with &lt;code&gt;-XX:+HeapDumpOnOutOfMemoryError&lt;/code&gt; (disabled by default) and &lt;code&gt;-XX:HeapDumpPath&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thread&lt;/td&gt;
&lt;td&gt;Too many threads, each with a stack&lt;/td&gt;
&lt;td&gt;Cap thread pools; reduce &lt;code&gt;-Xss&lt;/code&gt; only if the code allows it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;Metaspace growth from class loading, for example repeated redeploys or dynamic proxies&lt;/td&gt;
&lt;td&gt;Find the class loader leak; set &lt;code&gt;-XX:MaxMetaspaceSize&lt;/code&gt; to fail early with a clear error&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Code&lt;/td&gt;
&lt;td&gt;Code cache filling toward its maximum&lt;/td&gt;
&lt;td&gt;Check &lt;code&gt;-XX:ReservedCodeCacheSize&lt;/code&gt; against your budget&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GC&lt;/td&gt;
&lt;td&gt;Collector structures that scale with heap size&lt;/td&gt;
&lt;td&gt;Reduce heap, or leave more headroom&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nothing in NMT, but container grows&lt;/td&gt;
&lt;td&gt;Direct buffers or native code outside NMT&lt;/td&gt;
&lt;td&gt;Set &lt;code&gt;-XX:MaxDirectMemorySize&lt;/code&gt;; review JNI or native libraries&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Setting &lt;code&gt;-XX:MaxMetaspaceSize&lt;/code&gt; or &lt;code&gt;-XX:MaxDirectMemorySize&lt;/code&gt; does not remove a leak, but it turns a silent kernel kill into a Java &lt;code&gt;OutOfMemoryError&lt;/code&gt; with a message, which is much easier to diagnose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set limits that leave headroom
&lt;/h2&gt;

&lt;p&gt;Once you know the pools, size the container with a margin. A reasonable pattern is to set the heap as a percentage and leave the rest explicitly for non-heap memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resources:
  requests:
    memory: "1536Mi"
  limits:
    memory: "1536Mi"
env:
  - name: JAVA_TOOL_OPTIONS
    value: "-XX:MaxRAMPercentage=60 -XX:MaxMetaspaceSize=256m -XX:MaxDirectMemorySize=128m"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Line by line: the request equals the limit so the scheduler reserves what the pod can use; &lt;code&gt;MaxRAMPercentage=60&lt;/code&gt; gives a heap cap of about 921Mi (0.60 x 1536Mi); the metaspace and direct memory caps are bounded explicitly. These values are an illustration, not a recommendation. Replace them with the committed sizes you measured with NMT. Note that &lt;code&gt;Mi&lt;/code&gt; is a binary unit; &lt;code&gt;1536M&lt;/code&gt; would be a different, smaller amount.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Java &lt;code&gt;OOMKilled&lt;/code&gt; means the whole container exceeded its limit; it is not the same as a Java &lt;code&gt;OutOfMemoryError&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Confirm with &lt;code&gt;reason: OOMKilled&lt;/code&gt; and exit code 137 before investigating.&lt;/li&gt;
&lt;li&gt;Add up heap, stacks, code cache, metaspace and direct memory first; many kills are budget mistakes.&lt;/li&gt;
&lt;li&gt;Enable &lt;code&gt;-XX:NativeMemoryTracking=summary&lt;/code&gt; (5-10 percent overhead, restart needed), take a &lt;code&gt;baseline&lt;/code&gt;, then run &lt;code&gt;summary.diff&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;NMT does not see memory allocated by non-JVM native code.&lt;/li&gt;
&lt;li&gt;Cap metaspace and direct memory so failures are explicit and diagnosable.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Treat a Java &lt;code&gt;OOMKilled&lt;/code&gt; as an accounting problem. Work out the budget, measure which pool grows with NMT diffs, and then either fix the growth or give the container honest headroom. Repeat the diff after each change to confirm that the growing category is the one you fixed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://opshubs.space/tools/jvm-container-memory-calculator/" rel="noopener noreferrer"&gt;JVM Container Memory Calculator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://opshubs.space/tools/" rel="noopener noreferrer"&gt;OpsHubs ops calculators and tools&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://opshubs.space/java-oomkilled-in-kubernetes-find-the-memory-pool-with-nmt/" rel="noopener noreferrer"&gt;OpsHubs&lt;/a&gt;, where you can also find free &lt;a href="https://opshubs.space/tools/" rel="noopener noreferrer"&gt;ops calculators&lt;/a&gt;. This article was drafted with AI assistance and reviewed before publishing.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>java</category>
      <category>devops</category>
      <category>cloudnative</category>
    </item>
  </channel>
</rss>
