# JVM Memory on Kubernetes: How to Size Java Pods Without OOM Kills

> What a JVM holds inside a container, how it picks its heap and processors, and how to cut Java pod memory without OOM kills.

Source: https://levelfour.ai/blog/jvm-memory-on-kubernetes

Updated: 2026-11-23

November 23, 2026. By LevelFour Team.

Size a Java container's memory from its heap maximum plus what the JVM keeps outside the heap, and never lower its limits. Usage shows how far the heap has grown. `-Xmx` shows how far it can grow. Cut the request to a few quiet weeks of usage and the scheduler packs the node on that smaller number. On the first busy day the heap grows past the request, and if the node runs short of memory, the kubelet evicts the pods furthest over their requests, or the kernel's OOM killer acts first.

Three JVM behaviors set the right size. The heap grows toward its maximum as it fills. The JVM fixes that maximum and its garbage collector at start. It counts its processors from the CPU limit, or, with no CPU limit on a JDK that still reads CPU shares, from the CPU request. We checked the JVM facts against the OpenJDK documentation and source code in November 2026. For the product side, see [Java resource management](https://levelfour.ai/java-resource-management.md).

## What a JVM holds inside a container

The container's memory limit covers every process in the container, the page cache they bring in, files in a memory-backed `emptyDir` and kernel memory charged to it, such as socket buffers ([cgroup v2 memory](https://docs.kernel.org/admin-guide/cgroup-v2.html)). The Java heap is usually the largest part. When the container reaches its limit and the kernel can't reclaim enough, it OOM-kills the container; on cgroup v2 nodes, every process in it goes together ([Kubernetes resource management](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/)). Inside the JVM, five things share that limit:

- Heap: your objects. It starts at `-Xms` and grows toward `-Xmx` as it fills.
- Metaspace: class metadata, with no overall cap unless you set `-XX:MaxMetaspaceSize`. Only its class space stops at 1 GB by default (`-XX:CompressedClassSpaceSize`).
- Code cache: JIT-compiled code, up to 240 MB by default with tiered compilation.
- Thread stacks: each thread reserves 1 MB on Linux x64 and about 2 MB on arm64, and the container pays for the pages it touches. 300 threads reserve 300 MB on x64 and use a fraction of it.
- Direct buffers and other native memory: NIO buffers, which can grow as large as the heap unless you set `-XX:MaxDirectMemorySize`, the garbage collector's own structures, the JIT compiler's work memory and native libraries.

![The JVM section of a workload in LevelFour with the JVM reader on: an 8 GiB limit split into live set, heap above it and memory outside the heap, two weeks of heap by day, and the JVM's JDK, collector and time in GC pauses](https://levelfour.ai/assets/java-kubernetes-heap.10bdedde.webp)

With the JVM reader on, the workload drawer splits the container's memory and charts the heap day by day: here a 4 GiB heap over a live set of at most 1.17 GiB.

## How the JVM picks its heap in a container

Since JDK 10, and on JDK 8 from update 191, the JVM reads the container's memory and CPU limits instead of the node's ([JDK 8u191 release notes](https://www.oracle.com/java/technologies/javase/8u191-relnotes.html)). That first support covers cgroup v1. Nodes on current Kubernetes run cgroup v2: from 1.35 the kubelet won't start on cgroup v1 unless an administrator turns that check off ([Kubernetes on cgroup v2](https://kubernetes.io/docs/concepts/architecture/cgroups/)). The JVM reads cgroup v2 from JDK 15, 11.0.16, OpenJDK 8u372 and Oracle JDK 8u361 ([JDK-8230305](https://bugs.openjdk.org/browse/JDK-8230305)); an older JVM on a cgroup v2 node sizes itself from the whole node. With no `-Xmx`, the JVM sets the heap maximum as a share of the container's memory limit.

| Option | Default | What it sets |
| --- | --- | --- |
| `-Xmx` or `-XX:MaxHeapSize` | 25% of the memory limit (`-XX:MaxRAMPercentage`) | The most the heap can hold |
| `-Xms` or `-XX:InitialHeapSize` | 1.5625% of the memory limit through JDK 25 (`-XX:InitialRAMPercentage`) | The heap at start |
| `-XX:MaxMetaspaceSize` | No limit | Class metadata |
| `-XX:ReservedCodeCacheSize` | 240 MB | JIT-compiled code |
| `-Xss` | 1024 KB on Linux x64; on arm64, 2040 KB from JDK 21, 2048 KB before | Each thread's stack |

Sources: the java command reference for JDK 21, the JDK 26 release notes and the OpenJDK source.

A container with an 8 GiB limit and no heap options gets a 2 GiB heap and leaves 6 GiB to everything else. The 25% holds from about 500 MiB of limit up to about 120 GiB, where the default heap stops near 30 GiB so the JVM can keep compressed object pointers. Below 500 MiB the JVM takes about 125 MiB, or half the limit under 250 MiB (`-XX:MinRAMPercentage`). A container with no memory limit, and none set for its pod, gets 25% of the node's memory, up to the same cap. From JDK 26, a heap with no `-Xms` starts at its minimum size. Teams that want more set `-Xmx` or raise `MaxRAMPercentage`.

Either way the heap starts small. The collector grows the committed heap toward `-Xmx` as it needs room. Through JDK 25, G1 gives memory back only at its remark and full collections; from JDK 26 it also shrinks the heap after young collections when it has little collecting to do ([JDK-8238687](https://bugs.openjdk.org/browse/JDK-8238687)).

Set the heap on a plain `java` command line, as here, and anyone can read it from the pod spec, unless the image sets `_JAVA_OPTIONS`, which overrides the command line:

```yaml
containers:
  - name: checkout-api
    image: eclipse-temurin:21-jre
    command: ["java", "-Xms512m", "-Xmx4g", "-jar", "/app/checkout-api.jar"]
    resources:
      requests:
        cpu: "1"
        memory: 4762Mi
      limits:
        cpu: "2"
        memory: 8Gi
```

## Why rightsizing from usage OOM-kills JVMs

A JVM with `-Xmx4g` can run for weeks on 2.5 GiB of heap, because the JVM grows the heap as it fills. Size it from those weeks and you size it for a heap that hasn't grown yet. Then a batch job, a cache warm-up or a traffic spike pushes the heap toward 4 GiB. Cut the limit and the kernel kills the container. Cut only the request and the scheduler packs the node for the smaller number; when that node runs short of memory, the kubelet evicts first the pods using more than they requested ([node-pressure eviction](https://kubernetes.io/docs/concepts/scheduling-eviction/node-pressure-eviction/)).

Resizing a running JVM in place adds a second trap. The JVM fixes its heap maximum and its garbage collector when it starts. Lower a running pod's memory limit and the heap can still grow toward the old maximum, until the kernel OOM-kills the container at the new limit. From Kubernetes 1.34 the kubelet skips a decrease when usage is already above the new limit, but nothing stops the heap from growing afterward. Raise the limit and a heap sized as a percentage stays where it was until the JVM restarts. Both hold under the default resize policy; with a memory `resizePolicy` of `RestartContainer`, a memory resize restarts the container and the JVM sizes itself again. A resize also keeps the pod's QoS class, so a Guaranteed pod's memory request moves only with its limit, and a request cut under an unchanged limit can't happen in place ([resize container resources](https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/)).

## The CPU side: processors, GC threads and the collector

The JVM sizes its garbage collection threads and its ForkJoinPool from the number of processors it sees. With a CPU limit, it divides the limit's quota by its period and rounds up, so a 1500m limit counts as 2 processors ([OpenJDK source](https://github.com/openjdk/jdk21u/blob/master/src/hotspot/os/linux/cgroupUtil_linux.cpp)).

With no CPU limit, a JDK that still reads CPU shares counts from the CPU request: OpenJDK 8, and OpenJDK 11 and 17 before 11.0.17 and 17.0.5. Kubernetes turns the request into CPU shares, 1024 per core. On a cgroup v1 node the JVM divides the shares by 1024 and rounds up, except that it reads exactly 1024 shares, a 1000m request, as no request at all and counts every CPU. On cgroup v2 the container runtime turns the shares into a CPU weight and the JVM turns the weight back, so its count depends on the runtime's formula ([cgroup v2 CPU conversion](https://kubernetes.io/blog/2026/01/30/new-cgroup-v1-to-v2-cpu-conversion-formula/)). JDK 19 and later, JDK 17 from 17.0.5 (17.0.4 for Oracle JDK), JDK 11 from 11.0.17 and Oracle JDK 8 from 8u361 ignore the shares and count every CPU the container may run on, usually every CPU on the node ([JDK-8281181](https://www.oracle.com/java/technologies/javase/19-relnote-issues.html)). On a cgroup v1 node, OpenJDK 8 with no CPU limit and a memory limit of at least 1792 MiB goes from 2 processors to 1 when its request drops from 2000m to 900m, and from the Parallel collector to Serial at its next start.

Up to JDK 26, which includes the 21 and 25 LTS releases, the JVM also picks its collector from what it sees. With a single processor or less than 1792 MiB of memory it runs the Serial collector, which pauses the application and collects on one thread, instead of G1 ([JEP 523](https://openjdk.org/jeps/523)), or the Parallel collector on JDK 8 ([JDK 8 ergonomics](https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/ergonomics.html)). JDK 27 makes G1 the default everywhere. On the older JDKs, a cut below either line changes the collector at the JVM's next start, unless its options name one.

## How LevelFour sizes Java workloads

LevelFour puts those rules into every recommendation and every automated step for the containers it identifies as JVMs, in the namespaces you opt in on your [Kubernetes clusters](https://levelfour.ai/kubernetes.md):

- JVMs found by their signs: a JVM's name in the image or command line, a JVM-only option, or a variable such as `JAVA_TOOL_OPTIONS`, `JAVA_OPTS` or `JVM_OPTS` marks a container as one ([what the agent reads](https://docs.levelfour.ai/connectors/kubernetes/agent-permissions-and-data#what-it-sends)). The optional JVM reader also finds a HotSpot JVM whatever its image is called.
- Heap options read: `-Xmx`, `-Xms`, `-XX:MaxHeapSize` and `-XX:InitialHeapSize` from a plain `java` command line or a literal `_JAVA_OPTIONS`. Only the numbers leave the cluster.
- Heap measured, with the optional [JVM reader](https://docs.levelfour.ai/connectors/kubernetes/agent-permissions-and-data#the-jvm-reader) on: the heap each HotSpot JVM may grow to, an upper bound on its live set, committed heap, memory outside the heap and time in GC pauses, with its JDK version and collector. The reader is off by default and runs privileged pods in a namespace of its own. Its measurements start in shadow for every organization: LevelFour keeps them beside each recommendation and sizes nothing from them until it turns the measured heap on for yours, and then only after at least 72 hours that cover 90% of the time the agent saw the container ([the measured heap](https://docs.levelfour.ai/connectors/kubernetes/recommendations#the-measured-heap)).
- Findings, with the reader on: a heap left at the JVM's default 25% of a limit of 2 GiB or more, the Serial collector picked because the limits are small, a heap at least three times its live set, or a JDK that still reads CPU shares with no CPU limit.
- A memory floor on every cut: the heap maximum plus 256 MiB plus a tenth of the heap, or, once LevelFour trusts a measured heap, a fifth more than the most the JVM used beside the heap, if that is larger. A 4 GiB heap from the spec stops a cut at 4762 MiB, the number in the example above ([JVM heap rules](https://docs.levelfour.ai/connectors/kubernetes/recommendations#jvm-heap)). The floor stops a cut and never raises a request, so a request already under it stays where it is. It keeps the request above the heap; the limit holds the rest of what the JVM may use.
- No memory cut on an unknown heap: one set as a share of the limit, the JVM's default, or options from the image, a ConfigMap, a Secret, a shell wrapper or a variable such as `JAVA_TOOL_OPTIONS`. A trusted measured heap can lift that hold, but never for a JVM that can't see a memory limit (no limit, `-XX:-UseContainerSupport`, `MaxRAM` or a JDK that can't read cgroup v2) or one the reader can't read, such as OpenJ9.
- Limits kept: a JVM's limits never go down. A JVM with no CPU limit keeps its CPU request. Once a trusted measurement shows that the oldest JDK it ran ignores CPU shares, its recommendations size the CPU request like any other, but the automation component never cuts it in place.

![A JVM container's memory chart in LevelFour: the request steps down from 8 GiB to the 4.65 GiB memory floor in three rollouts, above the 4 GiB heap maximum, and the change is marked Verified](https://levelfour.ai/assets/java-kubernetes-rightsizing.6220ffbe.webp)

checkout-api runs with a 4 GiB heap: LevelFour takes its request from 8 GiB to the 4.65 GiB floor in three steps, and no lower.

Each change reaches the cluster in one of two ways. A pull request lands on the repository that deploys the workload, and your pipeline applies it once you merge it, or once LevelFour merges it under an auto-merge class you allowed; a cut sized from a trusted measured heap never merges on its own. Or, on Kubernetes 1.33 or later, the optional [automation component](https://docs.levelfour.ai/connectors/kubernetes/automation) resizes the running pods in place, in the namespaces your install allows and where automation is on. Each in-place cut takes at most 20% of the request. Until the step's interval passes, an OOM kill, an eviction, two restarts within an hour or a pod that stops being ready puts the pods back with no approval, to the last value that held through the workload's daily peak, and for 14 days the component cuts that resource no lower.

After OOM kills, the same component raises a JVM's memory in place. Two OOM kills of one container within an hour, or one while it crash loops, add up to 25% of memory, within your caps and never past double the workload's own spec. A JVM with no fixed `-Xmx` gets up to four times that step, because LevelFour allows for a heap that takes up to 75% of the new limit when the JVM restarts. HPA targets, NodePools and the rest of a cluster's savings are in [automated Kubernetes cost optimization](https://levelfour.ai/blog/automated-kubernetes-cost-optimization.md).

## Common mistakes

- A memory limit equal to `-Xmx`. A full heap then leaves no room for metaspace, threads and buffers.
- No heap options. A container with an 8 GiB limit runs a 2 GiB heap and pays for the rest.
- A JDK older than cgroup v2 on a current node. OpenJDK 8 before 8u372, Oracle JDK 8 before 8u361 and JDK 11 before 11.0.16 size their heap and processors from the whole node.
- A request cut to two quiet weeks of usage. The heap hasn't grown yet.
- CPU limits removed on an older JDK, then requests cut. OpenJDK 8, and OpenJDK 11 and 17 before 11.0.17 and 17.0.5, count their processors from the request, or on cgroup v2 from the weight the runtime derives from it.
- A limit lowered on a running JVM. The heap keeps the maximum it picked at start.

## Get started

[Start with LevelFour](https://levelfour.ai/sign-up) and install the agent with one Helm command, as the [Kubernetes connector docs](https://docs.levelfour.ai/connectors/kubernetes) show. You see usage within minutes and recommendations once a namespace you opt in has 7 days of usage. To measure the heap of JVMs whose spec doesn't show it, turn on the optional [JVM reader](https://docs.levelfour.ai/connectors/kubernetes/configuration#turn-on-the-jvm-reader); its measurements size nothing until LevelFour turns the measured heap on for your organization.

## Frequently asked questions

### How much memory does a Java container need on Kubernetes?

Its heap maximum plus what the JVM uses outside the heap: metaspace, up to 240 MB of code cache, the stack pages its threads touch, direct buffers and other native memory. LevelFour never cuts the memory request of a container it identifies as a JVM below the heap maximum plus 256 MiB plus a tenth of the heap. [Kubernetes pod rightsizing in the glossary](https://levelfour.ai/glossary/kubernetes-pod-rightsizing.md).

### Should the memory limit equal -Xmx?

No. The limit covers the whole container, and the heap is one part of it. With the limit at `-Xmx`, a full heap leaves no room for metaspace, threads and buffers, and the kernel OOM-kills the container.

### What is the default heap size of a JVM in a container?

25% of the container's memory limit, set by `-XX:MaxRAMPercentage`, for limits from about 500 MiB up to about 120 GiB, where it stops near 30 GiB. On cgroup v2, which nodes on current Kubernetes run, the JVM reads the limit from JDK 15, 11.0.16, OpenJDK 8u372 and Oracle JDK 8u361; an older JVM there takes 25% of the node's memory. The heap starts at 1.5625% of the limit through JDK 25, at its minimum size from JDK 26, and grows as it fills.

### Why does a Java pod get OOMKilled when its heap isn't full?

The kernel counts everything in the container against the memory limit: the heap pages the JVM touched, metaspace, code cache, thread stacks, direct buffers, other native memory, page cache and files in a memory-backed `emptyDir`. When the memory outside the heap grows, a half-empty heap can still push the container past the limit.

### Does LevelFour change my JVM options?

No. LevelFour sizes the container's requests around the heap you set and raises memory after OOM kills. Your `-Xmx`, `-Xms` and other options stay as you wrote them.

## See what LevelFour can save you

[Start free trial](https://levelfour.ai/sign-up) · [Get in touch](https://levelfour.cal.com/)

## About LevelFour

The FinOps platform that actually cuts costs. One system of record for Cloud, SaaS, and AI spend. LevelFour applies each saving, or your engineers merge it as code.

- **Cloud Savings**: 30% success fee on savings found and applied. No hidden charges, only pay on what you actually save.
- **Pro**: $30 per month · Up to $2,500 spend/mo
- **Scale**: $200 per month · Up to $7,500 spend/mo
- **Enterprise**: Custom · Unlimited spend

[Pricing](https://levelfour.ai/pricing.md) · [Guide for agents (llms.txt)](https://levelfour.ai/llms.txt) · [Documentation](https://docs.levelfour.ai) · MCP server: `https://mcp.levelfour.ai/mcp`
