Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

A Spring Boot service runs in a container with a 2 GiB memory limit and no JVM flags. How does the JVM pick its heap size, and why might it be either too small or too large?

A modern JVM reads the container's memory limit from cgroups and, with no flags, sets the maximum heap to 25 percent of it, so a 2 GiB container gets about 512 MiB of heap. That is often too small for a Spring Boot service and causes frequent garbage collection or OutOfMemoryError. If we fix it by setting the heap too large, heap plus the JVM's other memory exceeds the limit and the kernel kills the container, so we set -XX:MaxRAMPercentage around 60 to 75 or an explicit -Xmx and leave room for non-heap memory.

Why this question is asked

Memory is the setting that most often goes wrong when a Java service moves into containers. A container limit is a hard wall, and the JVM's defaults were designed for machines where Java shares RAM with many other programs. Interviewers ask this question to see whether we know how the JVM sizes itself, what else uses memory besides the heap, and how the two ways of failing look different. In this answer, we will recap container detection, calculate the default heap, see why it is often too small, look at the non-heap memory we must leave room for, and see what happens when the heap is too large.

1A Quick Recap: How the JVM Sees the Container

A container's memory limit is enforced by Linux control groups, cgroups for short, a kernel feature that caps the resources of a group of processes. Older JVMs ignored cgroups and read the host's total RAM, so a JVM in a 2 GiB container on a 64 GiB node would size its heap for 64 GiB and get killed. Since JDK 10, and backported to 8u191, the JVM has container support turned on by default (-XX:+UseContainerSupport). It reads the cgroup limit and treats it as the machine's physical memory. Support for cgroups v2, used by most current Linux distributions, arrived later, in JDK 15 and in updates of 11 and 8, so check your version if you run an older JDK.

We can see what the JVM detected with two commands run inside the container.

Shell
$ java -XshowSettings:system -version
Operating System Metrics:
Provider: cgroupv2
Memory Limit: 2.00G # the container limit was detected
$ java -XX:+PrintFlagsFinal -version | grep -E 'MaxHeapSize|MaxRAMPercentage'
size_t MaxHeapSize = 536870912 # 512 MiB
double MaxRAMPercentage = 25.000000

2How the Default Heap Is Calculated

Without -Xmx, the JVM takes MaxRAMPercentage of the memory it sees. The default is 25 percent, so 2 GiB x 0.25 = 512 MiB. For very small containers, below roughly 250 MB, a different rule applies (MinRAMPercentage, 50 percent by default), but for normal service sizes 25 percent is what we get. The initial heap is smaller still, about 1/64 of memory, and grows as needed.

The JVM also picks a garbage collector from what it sees. With at least two CPUs and about 1792 MB of memory it chooses G1; with less, it falls back to the Serial collector, which stops the application for every collection. So a container with one CPU gets a very different JVM from the same image with two.

3Why 512 MiB Is Often Too Small

The 25 percent default is cautious because on a shared machine Java should not take all the RAM. In a container, the JVM is usually the only real process, so three quarters of the limit sits unused while the heap is cramped. A typical Spring Boot service with caches, Hibernate metadata, and a few hundred concurrent requests may need 800 MiB to 1.2 GiB of heap to run comfortably.

With only 512 MiB, the garbage collector runs much more often, CPU goes into collection instead of requests, latency spikes appear, and under a burst the service may throw java.lang.OutOfMemoryError: Java heap space. Monitoring then shows the pod at 40 percent of its memory limit while the application struggles, which is confusing until we remember the default.

4Leave Room for Non-Heap Memory

The fix is to give the heap a larger share, but not all of it, because the JVM uses a lot of memory outside the heap. Metaspace holds class metadata, often 100 to 200 MiB for a Spring Boot application. Each thread has its own stack, up to 1 MiB reserved by default, and a busy service can have 200 threads. The code cache holds JIT-compiled code, up to 240 MiB reserved. Direct buffers, used by Netty and NIO, live outside the heap. GC data structures, native libraries and the C library's memory allocator take more.

YAML
# Kubernetes container spec
env:
- name: JAVA_TOOL_OPTIONS
value: "-XX:MaxRAMPercentage=70 -XX:+ExitOnOutOfMemoryError"
resources:
limits:
memory: 2Gi
# Result: MaxHeapSize = about 1.4 GiB, the rest for metaspace, threads and buffers
A 2 GiB container, heap at 70 percent
AreaTypical useNotes
Java heapup to 1.4 GiB-XX:MaxRAMPercentage=70
Metaspace150 MiBclass metadata
Thread stacks100 to 200 MiBabout 200 threads
Code cache50 to 100 MiBJIT-compiled code
Direct buffers, GC, native100 to 200 MiBNetty, NIO, malloc
Totalclose to 2 GiBlittle margin left
The heap is only one part of the process memory the kernel counts.

So we set the percentage explicitly, usually between 60 and 75 percent, or set -Xmx directly. We pass it in JAVA_TOOL_OPTIONS or the image's entrypoint so it does not depend on the command line.

A percentage scales with the limit when someone changes the pod size, which is why many teams prefer it to a fixed -Xmx. The right value depends on the non-heap use of the service, so we measure it with Native Memory Tracking rather than guess.

5Why Too Large Is Worse Than Too Small

If we set the heap to 90 percent, or -Xmx2g in a 2 GiB container, the heap alone can fill the limit, and the non-heap memory pushes the process over it. The kernel does not ask the JVM to free memory; its OOM killer simply kills the process with SIGKILL. Kubernetes reports the container as OOMKilled with exit code 137 and restarts it.

Notice the difference from a heap that is too small. A Java OutOfMemoryError is thrown inside the JVM, can be logged, and with -XX:+HeapDumpOnOutOfMemoryError leaves a heap dump to analyse. An OOMKill leaves nothing: no stack trace, no log line, no dump, just a restart. That is why we keep a margin and prefer a slightly smaller heap to a slightly larger one.

6Follow-up questions

Should we set the memory request equal to the limit?

For Java services, usually yes. The JVM sizes its heap from the limit, and the heap tends to grow towards its maximum over time, so the process will use close to the limit anyway. Setting the request equal avoids scheduling the pod on a node that cannot actually provide that memory.

Is -Xmx better than MaxRAMPercentage?

Both work. -Xmx is precise and easy to read, but it must be changed by hand when the container size changes. MaxRAMPercentage follows the limit automatically. If both are set, -Xmx wins. Teams often use a percentage in a shared base image and let a service set -Xmx when it has measured a specific need.

Why does the heap in metrics stay far below the pod's memory usage?

Because the pod's memory includes everything the process uses, metaspace, stacks, code cache, buffers and native allocations, plus page cache counted for the container. The heap is only one part, so a pod using 1.8 GiB with a 1.2 GiB heap is normal.

7Conclusion

In this answer, we saw that modern JVMs read the container's cgroup memory limit and, with no flags, set the maximum heap to 25 percent of it, about 512 MiB for a 2 GiB container, which is often too small for a Spring Boot service. We fix it with -XX:MaxRAMPercentage around 60 to 75 or an explicit -Xmx, always leaving room for metaspace, thread stacks, the code cache, direct buffers and native memory, because a heap that is too large gets the container killed by the kernel instead of producing a Java OutOfMemoryError.

What to say in the interviewAbout 80 seconds aloud

Open with

Since JDK 10, and 8u191, the JVM has container support on by default, so it reads the cgroup memory limit and treats 2 GiB as the machine's memory.

Then, if they want more

With no flags, the maximum heap is MaxRAMPercentage of that, and the default is 25 percent, so about 512 MiB. That is often too small for a Spring Boot service, which can need close to a gigabyte under load, so we see lots of GC, latency spikes or OutOfMemoryError while the pod looks only half used.

The fix is to set MaxRAMPercentage, typically 60 to 75 percent, or an explicit Xmx, through JAVA_TOOL_OPTIONS. But we cannot give the heap everything, because the JVM uses memory outside the heap: metaspace for classes, a stack per thread, the code cache, direct buffers for Netty or NIO, and native allocations.

If heap plus all of that goes over the limit, the kernel's OOM killer sends SIGKILL, the pod shows OOMKilled with exit code 137, and there is no Java error or heap dump at all. So too large is actually worse than too small. I would start around 70 percent, measure with Native Memory Tracking, and keep a margin.

It is also worth knowing that with fewer than two CPUs the JVM picks the Serial collector.

About this answer

Last updated
12 November 2026
Versions it names
Java 10, Java 15
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.
Official documentation
Kubernetes documentation
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 "A Spring Boot service runs in a container with a 2 GiB memory limit and no JVM flags....": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/06c58d2352/