Memory
Containers: What the JVM Sees
The same image under two limits gave 1.94 GB of heap and G1, or 128 MB and SerialGC. A container limit is an input to ergonomics, not a ceiling applied afterwards.

On this page
The same image, two sets of limits. Unlimited: MaxHeapSize 2,080,374,784 and G1. Under --cpus=1 -m 512m: 134,217,728 and SerialGC.
Nothing in the image changed. Two numbers in a run command decided how much heap the process would have and which garbage collector it would use, and the second of those decisions carries consequences an operator would not connect to a CPU limit.
The runs: eclipse-temurin:26-jdk (openjdk 26.0.2, Temurin-26.0.2+10) under Docker, from a host running JDK 26.0.2.1 (Homebrew build) on macOS 26.6.2.
The limit is an input, not a ceiling
Running the same class under five configurations and printing what the runtime concluded:
| Limits | availableProcessors() |
maxMemory() |
common pool |
|---|---|---|---|
| none | 12 | 2,080,374,784 | 11 |
--cpus=1 -m 512m |
1 | 134,152,192 | 1 |
--cpus=1.5 -m 512m |
2 | 134,152,192 | 1 |
--cpus=2 -m 1g |
2 | 268,369,920 | 1 |
--cpus=4 -m 2g |
4 | 536,870,912 | 3 |
The heap figures are not heuristics. MaxRAMPercentage defaults to 25, the trace shows the detected memory limit for -m 512m as 536,870,912 bytes, and a quarter of that is 134,217,728 — the exact MaxHeapSize the flag dump reports. Two gigabytes gives exactly 536,870,912.
maxMemory() reads 65,536 bytes lower than MaxHeapSize. The two are not the same quantity — one is the flag the ergonomics set, the other is what the runtime reports it will attempt to use — and the gap is worth not mistaking for a rounding error when reconciling a dashboard against a flag.
The common pool column is the one that propagates furthest. It is availableProcessors() minus one, floored at one, and it sizes every parallelStream(), every CompletableFuture without an explicit executor, and a good deal of library code that never asked the deployment what it wanted.
The JVM does not do fractions
--cpus=1.5 produced an availableProcessors() of 2. The detection trace shows exactly how:
[0.000s][trace][os,container] CPU Quota is: 150000
[0.000s][trace][os,container] CPU Period is: 100000
[0.000s][trace][os,container] CPU Quota count based on quota/period: 2
[0.000s][trace][os,container] OSContainer::active_processor_count: 2
The quota is one and a half periods and the runtime rounds the division up. A pod granted 1.5 CPUs runs a JVM that believes it has two, sizes its pools for two, and is then throttled by the scheduler for using them — which is the subject of a later part of this series and, for now, a reason to prefer whole numbers in a manifest.
One CPU changes the collector
The row worth dwelling on is --cpus=1 -m 512m, where UseSerialGC reads true {ergonomic} and UseG1GC reads false. The ergonomics select a different collector, and nothing in the deployment mentioned garbage collection.
Both limits are involved, and running the matrix isolates them. One CPU gives SerialGC at every memory size tested up to 4 GB. Two or more CPUs give G1 only above a memory threshold, and the threshold is sharp: at --cpus=4, 1790 MB selects Serial and 1792 MB selects G1. G1 requires two conditions — at least two processors and at least 1792 MB — and failing either one is enough.
That is worth knowing because the second condition catches deployments that look generous. A pod with four CPUs and a 1 GB limit runs SerialGC, and nothing about four CPUs suggests it would.
That is where this connects to something already measured here. Under SerialGC, counted-loop safepoint polls are off by default — which means a single thread in a long counted loop can stall every other thread for far longer than any GC log will report. A one-CPU limit is the first link in that chain, and it is set by somebody sizing a pod rather than by anybody thinking about safepoints.
It also means the collector comparison a team performed before deploying may describe a collector the deployment does not run.
Reading the detection
None of this needs to be inferred. -Xlog:os+container=trace prints the whole decision:
[0.000s][debug][os,container] Detected cgroups v2 unified hierarchy
[0.000s][debug][os,container] OSContainer::init: is_containerized() = true because
all controllers are mounted read-only (container case)
[0.001s][trace][os,container] Memory Limit is: 536870912
The second line is the one to notice. The JVM is not told it is in a container; it infers it, here from the controllers being mounted read-only. Detection is a conclusion drawn from the filesystem, which is why it can be wrong in unusual sandboxes and why checking it beats assuming it.
The trace also names the files, which is the part worth knowing when a number looks wrong:
[0.000s][debug][os,container] Detected optional cpuset controller entry in
/sys/fs/cgroup/cgroup.controllers
[0.000s][trace][os,container] Path to /cpu.max is /sys/fs/cgroup/cpu.max
[0.000s][trace][os,container] Path to /memory.max is /sys/fs/cgroup/memory.max
Two files decide almost everything above. cpu.max holds the quota and the period on one line; memory.max holds the byte limit. Both can be read directly from inside a running container, which turns “what does the JVM think it has” into a question with an answer that does not require restarting anything.
The unlimited row deserves one correction as well. Its 2,080,374,784 bytes of heap implies a detected memory limit of about 7.75 GiB, and the host this ran on has 24 GB. “No limit” meant the Docker virtual machine’s memory, not the machine’s — a distinction that matters on any developer laptop where a container is sized against a VM nobody configured deliberately.
Startup is also where all of this happens — the limits are read before the first application class is loaded, alongside the class loading work that startup already consists of.
Part one of four
This part exists to make the next three short. Once detection is understood, heap sizing stops being a question about cgroups and becomes a question about the gap between a heap and a resident set. Native memory becomes a question about what the 75 per cent outside MaxRAMPercentage is being spent on. Thread counts become a question about one number the runtime already printed.
The command that answers all three for a given deployment is one flag on one run of the real image under the real limits. Anything derived from the host’s nproc describes a machine the process will never see.
Frequently asked
- Does the JVM respect my container memory limit?
- It reads it at startup and derives the heap from it. On JDK 26 with -m 512m the runtime detected a memory limit of 536,870,912 bytes and set MaxHeapSize to 134,217,728 - a quarter of it, because MaxRAMPercentage defaults to 25. The limit is an input to that calculation, not a cap enforced afterwards.
- What does --cpus=1.5 give me?
- Two. The JVM divides the cgroup quota by the period and the result is a whole number of processors - the trace prints CPU Quota is 150000, CPU Period is 100000, and CPU Quota count based on quota/period 2. Fractional CPU limits round up as far as the runtime is concerned.


