Quick revision
The short answer
The policy lists every part of a JVM's memory, sets the container's memory request equal to its limit, and sizes the heap as a measured percentage of the limit, usually between half and three quarters, leaving the rest for non-heap areas. Guard limits such as MaxMetaspaceSize and MaxDirectMemorySize make leaks fail with a Java error instead of a kernel kill, each service class gets a measured baseline from native memory tracking, alerts fire as resident memory nears the limit, OOMKilled restarts are tracked fleet-wide, and the whole policy ships in the shared base image and is reviewed on every JDK upgrade.
What to remember
In this answer, we built a memory budget policy that lists every part of JVM memory, sets request equal to limit and sizes the heap with a measured MaxRAMPercentage, usually between half and three quarters. Guard limits on metaspace and direct memory turn leaks into clear Java errors, baselines from NMT give each service class expected numbers, alerts on resident memory and a fleet metric of OOMKilled restarts catch drift, and the policy ships in the base image and is reviewed with each JDK upgrade.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The policy lists every part of a JVM's memory, sets the container's memory request equal to its limit, and sizes the heap as a measured percentage of the limit, usually between half and three quarters, leaving the rest for non-heap areas. Guard limits such as MaxMetaspaceSize and MaxDirectMemorySize make leaks fail with a Java error instead of a kernel kill, each service class gets a measured baseline from native memory tracking, alerts fire as resident memory nears the limit, OOMKilled restarts are tracked fleet-wide, and the whole policy ships in the shared base image and is reviewed on every JDK upgrade.
Why this question is asked
When dozens of teams run JVMs on Kubernetes, kernel OOM kills tend to become a background noise of mysterious restarts. Each team fixes its own symptoms by raising limits or lowering -Xmx, and nobody knows what a JVM service should actually need. A memory budget policy replaces this with a shared model. Interviewers ask this design question to see whether we can turn JVM memory knowledge into a standard that teams can follow without being experts. In this answer, we will list the components that must fit into the limit, set the container and heap rules, add guard limits, define how baselines are measured, design the monitoring, and decide where the policy lives.
1What Must Fit Inside the Container Limit
The kernel counts the resident memory of the whole process against the container's limit, so the budget has to list everything, not just the heap. A useful policy writes down each component with a typical share and how it is controlled.
| Component | Typical size | Controlled by |
|---|---|---|
| Java heap | 1,200 MB (60%) | MaxRAMPercentage |
| Metaspace and class space | 100-250 MB | MaxMetaspaceSize (guard) |
| Code cache | 50-150 MB | ReservedCodeCacheSize |
| Thread stacks | 50-300 MB | Thread pool bounds, virtual threads |
| GC structures | 3-10% of heap | Collector choice, heap size |
| Direct buffers | 0-200 MB | MaxDirectMemorySize (guard) |
| Native libraries and malloc | 50-300 MB | Measurement, MALLOC_ARENA_MAX |
| Headroom | at least 10% of the limit | The rest of the limit |
2Container and Heap Rules
First, memory request equals memory limit for every JVM pod. The JVM sizes itself from the limit and does not give memory back quickly, so a pod that uses more than its request is exposed to eviction and to the OOM killer when the node is busy. Equal values make the budget a guarantee.
Second, the heap is a percentage of the limit, not a fixed -Xmx. MaxRAMPercentage makes the same image work at 1 GiB and 8 GiB and keeps the non-heap share explicit. The policy names a default, such as 60 percent, and allows a range of 50 to 75 percent per service class, chosen from measurement. The default when nothing is set is only 25 percent, which wastes most of the container, so the policy always sets it.
3Guard Limits That Turn Kills Into Errors
Self-sizing areas are convenient, but a leak in one of them grows until the kernel kills the process, which leaves no Java error and no hint of the cause. A guard limit is a cap set well above normal use, so it never affects a healthy service but makes a leak fail early with an error that names the area.
The policy sets MaxMetaspaceSize at two to three times the measured post-warmup size, MaxDirectMemorySize at a value that covers the service's networking libraries, and an explicit ReservedCodeCacheSize. It also bounds thread pools and sets MALLOC_ARENA_MAX, because those areas have no single JVM limit.
ExitOnOutOfMemoryError makes the JVM stop on the first OutOfMemoryError, so Kubernetes restarts a clean process instead of leaving a half-broken one serving traffic.
4Measuring a Baseline Per Service Class
Numbers in a policy are only useful if they are measured. Each service class, for example web API, stream consumer and batch worker, gets a baseline from staging under realistic load: run with -XX:NativeMemoryTracking=summary, take jcmd VM.native_memory summary after warmup and after a few hours, and record committed memory per category next to the container's resident memory. The difference between NMT's total and resident memory is the native library and allocator share, and it is recorded too.
The output is a short table per service class: expected resident memory, heap percentage, guard values and headroom. A new service starts from its class's numbers and measures its own before production.
5Monitoring and Fleet Metrics
The policy defines two alerts and one fleet metric. The first alert fires when a container's working set stays above 90 percent of its limit for ten minutes, which gives time to act before a kill. The second fires on any OOMKilled restart. The fleet metric counts OOMKilled restarts per week across all services and is reviewed in the platform team's regular operations review, so a rising trend is noticed even when each team only sees one restart.
Each service also exports heap after GC, metaspace, direct buffer use and code cache use through its metrics endpoint, so the area that grew can be found without logging into a pod.
- Working set above 90% of limitAlert fires before the kernel acts
- Which area grew?Compare heap, metaspace, direct, code with the baseline
- Inside NMT categories?Java-managed areaYesGuard limit or code fix for that areaNoCheck native libraries and malloc arenas
- Update the service's budgetRecord new measured numbers
4 steps in order. Play them, or step through them one at a time.
6Where the Policy Lives
A policy in a wiki page is ignored; a policy in the base image is applied. The shared Java base image carries JAVA_TOOL_OPTIONS with the default percentage and guard limits, the MALLOC_ARENA_MAX setting and the GC logging options, so every service gets them by rebuilding. Teams override a value only through a documented variable, with the reason recorded.
The platform team reviews the policy on every JDK upgrade, because defaults and memory behaviour change between releases, and re-runs the baselines for each service class before rolling the new image out.
7Follow-up questions
When would you deviate from the standard percentage for a service?
When measurement shows the non-heap share is very different from the typical service. Small containers of 1 GiB or less need a lower percentage, often 50, because fixed overhead such as metaspace and the code cache takes a bigger share of the limit. Services with heavy off-heap use, such as Netty servers with large direct pools, Kafka Streams with RocksDB or search services with memory-mapped indexes, also need a lower heap share. Large services with modest non-heap use, such as a 16 GiB in-memory cache, can go to 75 or even 80 percent. In each case the deviation is recorded with the measurement that justifies it.
Why not simply give every pod a much larger limit?
It hides the problem and wastes capacity. Leaks still grow until they hit the new limit, and the extra reservation multiplies across hundreds of pods. A measured budget with guard limits costs less and fails more clearly.
Does this change with virtual threads?
The components stay the same, but the balance moves: thread stacks shrink and heap use may grow, so services that switch should re-measure their baseline and may adjust the heap percentage upwards.
8Conclusion
In this answer, we built a memory budget policy that lists every part of JVM memory, sets request equal to limit and sizes the heap with a measured MaxRAMPercentage, usually between half and three quarters. Guard limits on metaspace and direct memory turn leaks into clear Java errors, baselines from NMT give each service class expected numbers, alerts on resident memory and a fleet metric of OOMKilled restarts catch drift, and the policy ships in the base image and is reviewed with each JDK upgrade.
Open with
I would start by writing down everything that counts against the container limit: the heap, metaspace and class space, the code cache, thread stacks, GC structures, direct buffers, and native libraries and malloc arenas.
Then, if they want more
Then two rules. Memory request equals limit for every JVM pod, so the budget is guaranteed. And the heap is a percentage of the limit through MaxRAMPercentage, never a hard-coded Xmx, with a default around 60 percent and a range of half to three quarters picked from measurement per service class.
Next, guard limits: MaxMetaspaceSize, MaxDirectMemorySize and an explicit code cache size set well above normal use, so a leak fails with a clear Java error instead of a kernel kill, plus bounded thread pools and MALLOC_ARENA_MAX. ExitOnOutOfMemoryError makes sure a broken process restarts cleanly.
Each service class gets a baseline from staging using native memory tracking, recording committed memory per category and the gap to RSS. For monitoring, I would alert when the working set stays above 90 percent of the limit and on any OOMKilled restart, and track OOMKilled restarts as a fleet-wide weekly metric.
Finally, the policy lives in the shared base image as default JVM options, overrides need a recorded reason, and the platform team re-measures and reviews it on every JDK upgrade.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Define a memory budget policy for JVM services running in Kubernetes. It should cover...": https://scrivio-interview-library.netlify.app/java/memory-layout/22b5de82dd/