Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does a modern JVM detect container limits? If you run a JVM without -Xmx in a pod with a 1 GiB memory limit and 2 CPU limit, what heap and processor count will it use?

A modern JVM has container support on by default (UseContainerSupport): at startup it reads the memory and CPU limits from the Linux cgroup files, cgroup v1 or v2, and uses those instead of the host's resources. In a pod with a 1 GiB memory limit and a 2 CPU limit, and no -Xmx, the maximum heap is MaxRAMPercentage, 25 percent of the limit, so about 256 MB, and Runtime.availableProcessors() returns 2, which in turn sizes GC threads, JIT compiler threads and the ForkJoinPool common pool. We can confirm this with java -XshowSettings:system -version or -Xlog:os+container, and for a dedicated service we usually set MaxRAMPercentage explicitly because 25 percent is too low.

Why this question is asked

Almost every Java service now runs in a container, and the JVM sizes many things from the resources it believes it has. If it reads the wrong numbers, it creates the wrong heap and the wrong number of threads. Interviewers ask this question because the default answer surprises people: a service in a 1 GiB pod gets only a quarter of it for the heap. In this answer, we will recap what cgroups are, follow how the JVM reads the limits, calculate the heap and processor count for the example pod, check what the JVM sees, and fix the default heap percentage.

1A Quick Recap: Containers and cgroups

A container is a normal Linux process with restrictions. The restriction on resources comes from control groups, or cgroups, a kernel feature that limits how much memory and CPU a group of processes can use. Kubernetes turns a pod's resources.limits into cgroup settings. The memory limit becomes a hard ceiling: if the processes use more, the kernel kills one of them. The CPU limit becomes a quota: for example 2 CPUs means the container may use 200 ms of CPU time in every 100 ms period across all its threads.

There are two versions of cgroups. Version 1 keeps each resource in its own hierarchy, for example /sys/fs/cgroup/memory/memory.limit_in_bytes. Version 2, used by current Linux distributions and Kubernetes nodes, has one unified hierarchy with files like /sys/fs/cgroup/memory.max and cpu.max. The JVM must understand the version the node uses, otherwise it falls back to reading the whole host.

2How the JVM Reads the Limits

Since JDK 10, and in later JDK 8 updates, the flag UseContainerSupport is on by default on Linux. Early in startup the JVM checks which cgroup version is mounted, finds its own cgroup, and reads the memory limit and the CPU quota and period. From the quota it computes the number of CPUs it may use, rounding up: a quota of 200000 microseconds per 100000 microsecond period gives 2. If no limit is set, it falls back to the host's memory and CPU count.

Step throughWhat the JVM does at startup in a pod
  1. UseContainerSupport on?
    YesDetect cgroup v1 or v2NoUse host memory and CPUs
  2. Read memory limitmemory.max = 1073741824 (1 GiB)
  3. Read CPU quotacpu.max = 200000 100000 -> 2 CPUs
  4. Ergonomicsheap = 25 percent of 1 GiB, threads from 2 CPUs
  5. Application startsavailableProcessors() = 2

5 steps in order. Play them, or step through them one at a time.

3Working Out the Example: 1 GiB and 2 CPUs

Without -Xmx, the maximum heap is set by MaxRAMPercentage, whose default is 25. So 25 percent of 1 GiB is 256 MB. The initial heap is set by InitialRAMPercentage, which defaults to 1.5625 percent, about 16 MB, so the heap starts small and grows. For very small memory sizes a different rule, MinRAMPercentage, applies, but that is not our case. There is one more surprise: ergonomics only treats a machine as server class, and picks G1, when it has at least two CPUs and roughly 1792 MB of memory or more. A 1 GiB pod is below that, so this JVM runs the Serial collector unless we name a collector explicitly.

Java
// inside the pod (limits: memory 1Gi, cpu 2), no -Xmx
Runtime rt = Runtime.getRuntime();
System.out.println(rt.availableProcessors()); // 2
System.out.println(rt.maxMemory() / (1024 * 1024)); // about 256 (MB)
System.out.println(ForkJoinPool.commonPool().getParallelism()); // 1 (processors - 1)

The processor count matters far beyond this method call. The JVM uses it to decide how many parallel GC threads to start (2 here), how many JIT compiler threads to run, and the ForkJoinPool common pool uses processors minus one, so parallel streams and CompletableFuture's default async methods get one worker. Many libraries and frameworks size their own pools from availableProcessors() too, for example Netty's event loops. So a correct number here keeps the whole process in proportion to its real CPU budget.

4Checking What the JVM Sees

We do not have to calculate this by hand. On Linux, -XshowSettings:system prints the container metrics the JVM detected, and unified logging with the os+container tags shows the detection step by step.

Shell
java -XshowSettings:system -version
# Operating System Metrics:
# Provider: cgroupv2
# Effective CPU Count: 2
# CPU Period: 100000us
# CPU Quota: 200000us
# Memory Limit: 1.00G
java -Xlog:os+container=info -version
# [0.002s][info][os,container] Memory Limit is: 1073741824
# (exact lines vary by JDK version)

If the Provider line is missing or the memory limit shows Unlimited although the pod has a limit, the JVM does not understand this node's cgroup setup, usually because the JDK is too old for cgroup v2. That is a separate and serious problem, because the JVM would then size itself from the whole node.

5Why We Usually Set MaxRAMPercentage

The 25 percent default comes from the time when a JVM shared a big machine with many other processes. In a pod, the service is usually the only process, so leaving 75 percent of the memory for non-heap use wastes most of it. A 1 GiB pod running Spring Boot with a 256 MB heap may suffer frequent GCs while 500 MB sits unused.

So for a dedicated service we set the percentage explicitly, typically between 60 and 80 percent depending on how much native memory the service uses (metaspace, thread stacks, direct buffers, code cache). For example -XX:MaxRAMPercentage=70 gives about 717 MB of heap in the 1 GiB pod and leaves about 300 MB for everything else. We then confirm with a load test that RSS stays below the limit. Setting a percentage rather than -Xmx keeps the heap correct when someone later changes the pod size.

6Follow-up questions

Does -Xmx still work in a container?

Yes. An explicit -Xmx overrides the percentage rules completely. The risk is that it no longer follows the pod: if the limit is lowered below the heap plus non-heap memory, the kernel kills the pod. That is why percentages are preferred in containers.

What if the pod has a CPU request but no CPU limit?

Then there is no quota, and on current JDKs the JVM sees the CPUs available to the container, which can be the whole node. Older releases also used CPU shares derived from the request, but that behaviour changed in later releases, so check your version. If needed, -XX:ActiveProcessorCount sets the number explicitly.

Is container support available in Java 8?

Yes, from later JDK 8 updates, and cgroup v2 support was added later still in a newer 8 update. Very old builds read the host values, so check your vendor's release notes for the exact update.

7Conclusion

In this answer, we saw that cgroups carry a pod's limits, and that a modern JVM reads them by default through UseContainerSupport, for both cgroup v1 and v2. In a pod with 1 GiB and 2 CPUs and no -Xmx, the heap maximum is 25 percent, about 256 MB, and availableProcessors() returns 2, which sizes GC threads, JIT threads and the common pool. We checked the detection with -XshowSettings:system and -Xlog:os+container, and set MaxRAMPercentage explicitly because 25 percent is too low for a dedicated service.

What to say in the interviewAbout 90 seconds aloud

Open with

Modern JVMs have container support on by default, the UseContainerSupport flag. At startup the JVM detects whether the node uses cgroup v1 or v2, reads its own cgroup's memory limit and the CPU quota and period, and uses those instead of the host's resources.

Then, if they want more

So in a pod with a 1 gigabyte memory limit and a 2 CPU limit and no Xmx, the maximum heap comes from MaxRAMPercentage, which defaults to 25 percent, so about 256 megabytes. The initial heap is about 1.5 percent, around 16 megabytes. availableProcessors returns 2, because the quota is 200 milliseconds per 100 millisecond period.

That number drives a lot: parallel GC threads, JIT compiler threads, the ForkJoinPool common pool, which gets processors minus one, and many library thread pools. And because 1 gigabyte is below the server-class memory threshold of about 1.8 gigabytes, ergonomics picks the Serial collector, not G1, unless I set one.

To check what the JVM actually sees, I run java -XshowSettings:system -version, which prints the provider, the effective CPU count and the memory limit, or use -Xlog:os+container.

And in practice I'd set MaxRAMPercentage explicitly, somewhere around 70 to 75 percent for a dedicated service, because 25 percent wastes most of the pod, and then confirm under load that RSS stays below the limit.

About this answer

Last updated
12 November 2026
Versions it names
Java 8, Java 10
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 "How does a modern JVM detect container limits? If you run a JVM without -Xmx in a pod...": https://scrivio-interview-library.netlify.app/java/jvm-tuning/5c80eb4ff1/