During triage of a HotSpot JVM in a Kubernetes pod, how do you check which CPU count and memory size the JVM believes it has, and how does JDK 25 derive them?
answer
- ask the JVM, not the pod spec
- the container section of VM.info
- quota over period, rounded up
- requests do not count
- the memory limit becomes physical memory
basics
~20 sAsk the JVM: the container section of jcmd <pid> VM.info shows its CPU count and limits. On JDK 25 the count is quota over period, rounded up and capped by usable CPUs; the memory limit stands in for physical memory.
solid answer
~40 sDo not infer it from the pod spec; read what the JVM decided. `jcmd <pid> VM.info` prints a `container (cgroup) information:` section with `active_processor_count`, `cpu_quota`, `cpu_period`, `cpu_shares` and `memory_limit_in_bytes`, and `-Xlog:os+container=debug` at startup says whether the JVM considers itself containerized at all. With `UseContainerSupport` on (the Linux default), JDK 25 takes the CPUs the process may run on and caps them at the quota divided by the period, rounded up: a 1500m limit gives 2. CPU shares, which a Kubernetes request sets, are printed but ignored, so a pod with a request and no limit sees every CPU it may run on. `-XX:ActiveProcessorCount` overrides all of it. For memory, the container limit replaces physical memory, so ergonomic heap sizing, such as `MaxRAMPercentage` at its 25 percent default, works from the limit.
code
bash · 1 linejcmd 1 VM.info | sed -n '/^container (cgroup) information:/,/^$/p'go deeper
Know that a JVM in a container reads its CPU and memory limits, and that jcmd VM.info shows what it concluded.
Explain the CPU arithmetic, quota over period rounded up and capped by usable CPUs, and that the memory limit stands in for physical memory.
Establish the JVM's view before tuning anything: read the container section, spot a request-only pod or an override, and connect the count to the pools it sized.
Decide whether the platform sets CPU limits, explicit ActiveProcessorCount values or neither, because that choice silently sizes every JVM's pools across the fleet.
## Why this is triage evidence Thread pools, garbage-collector worker counts, the common fork/join pool and the default maximum heap are all derived at startup from two numbers: how many CPUs the JVM thinks it has, and how much memory it thinks the machine has. When a service in a pod behaves differently from the same build elsewhere, those two numbers are among the first things to establish. The pod spec says what Kubernetes asked for; only the JVM can tell you what it concluded. ## How to ask the JVM - `jcmd <pid> VM.info` prints, among much else, a section headed `container (cgroup) information:`. Its first lines are `container_type`, `cpu_cpuset_cpus`, `cpu_memory_nodes`, `active_processor_count`, `cpu_quota`, `cpu_period`, `cpu_shares`, `cpu_usage_in_micros`, then the memory lines starting with `memory_limit_in_bytes`, in kilobytes with a `k` suffix. - `-Xlog:os+container=debug` at startup logs `OSContainer::init: is_containerized() = true` or `false`, with the reason; `trace` level adds the quota arithmetic. - `Runtime.getRuntime().availableProcessors()` in the application returns the same CPU count the VM uses. - `jcmd <pid> VM.flags` shows the values ergonomics chose from those inputs, such as the maximum heap size. The section also appears in a fatal error log, so a crashed JVM leaves the same evidence behind. ## When the JVM considers itself containerized `UseContainerSupport` defaults to `true` on Linux. With it on, JDK 25 treats the process as containerized when all cgroup controllers are mounted read-only, the normal situation inside a container, or when any CPU or memory limit is set. With `-XX:-UseContainerSupport`, or when neither holds, the JVM uses the host's figures. In triage this has two readings: - `container information not found.` in `VM.info` means the JVM is working from the host's figures; look for container support switched off on the command line or in `JAVA_TOOL_OPTIONS`. - `container_type: cgroupv1` or `cgroupv2` says which hierarchy the numbers came from, which matters when you compare them with the files under `/sys/fs/cgroup` yourself. ## The CPU count, step by step 1. Start from the CPUs the process may run on (its affinity mask, which reflects any cpuset). 2. If a CPU quota is set, compute `ceil(quota / period)`. 3. Take the smaller of the two. 4. If `-XX:ActiveProcessorCount` is greater than zero, use it instead, whatever the cgroup says. | Pod CPU settings, 16-CPU node | `active_processor_count` | |---|---| | limit 1500m | 2 | | limit 500m | 1 | | request 4, no limit | 16 | | limit 1500m, `-XX:ActiveProcessorCount=8` | 8, printed as `2, but overridden by -XX:ActiveProcessorCount 8` | CPU **shares**, which Kubernetes derives from the CPU **request**, appear in the `cpu_shares` line but play no part in the count on JDK 25; earlier releases weighed them. The count is cached and refreshed after a short interval, but pools and GC thread counts sized at startup stay as they were. ## The memory size When the JVM is containerized and a memory limit is set, that limit **replaces physical memory** for its ergonomics. With no `-Xmx`, the default maximum heap is a percentage of it: `MaxRAMPercentage`, 25 by default, or `MinRAMPercentage`, 50 by default, when the memory is small. With no memory limit, the JVM sizes itself from the node's memory instead, which is the triage clue when a heap is far larger than the pod was meant to have. ## Reading it in an incident - A count of 1 or 2 on a service tuned for many cores points at a low CPU limit, and every derived pool is small. - A count equal to the node's CPUs with only a request set means pools sized for the whole node, all competing for the CPU time the pod actually gets. - A memory limit line of `unlimited` means the heap default came from the node. - An unexpected `but overridden by -XX:ActiveProcessorCount` names a flag someone set, often through an environment variable such as `JAVA_TOOL_OPTIONS`. What to do about a wrong count (pool sizing, throttling, heap budgets) belongs with tuning; the triage step is establishing what the JVM believed.
- A pod requests 4 CPUs and sets no limit on a 32-CPU node. What does the JVM see, and why does it matter?On JDK 25 it sees 32, or fewer if a cpuset restricts it: shares from the request are ignored and no quota caps the count. Garbage-collector threads, the common fork/join pool and any pool sized from `availableProcessors()` are built for 32 CPUs while the pod is scheduled for 4, which is worth knowing before reading any latency evidence.
- When would you set -XX:ActiveProcessorCount explicitly?When the derived count is wrong for the workload and the pod spec cannot express the right one, for example a pod with no CPU limit where you still want pools sized for its request. It overrides the cgroup arithmetic completely, and `VM.info` shows both numbers, so the override is visible during triage.
- The JVM uses the CPU request from the pod spec as its CPU count.
- A 1500m CPU limit makes the JVM see one CPU.
- Inside a container the JVM always sizes its heap from the node's memory.
- -XX:ActiveProcessorCount only caps the count the cgroup computes.
- Runtime.availableProcessors() reports the node's CPUs whatever the limits.
Assumes JDK 25 on Linux with cgroups; the CPU count ignores CPU shares there, while earlier releases also weighed them.