Quick revision
The short answer
A good policy sets memory request equal to limit, sizes the heap with MaxRAMPercentage from a standard default (for example 70 percent) with documented, measured exceptions, gives services at least two CPUs so G1 and the JIT have parallelism, and makes a deliberate choice about CPU limits: many teams use requests only, or generous limits, and watch throttling. It also sets a minimum JDK version that handles cgroup v2 correctly, and provides dashboards for CFS throttling, RSS against the limit and OOMKilled counts so the rules can be checked against reality.
What to remember
In this answer, we wrote a container sizing policy for JVM services: memory request equal to limit so the scheduler and the JVM see the same stable number, heap through MaxRAMPercentage with a measured default and documented exceptions, at least two CPUs so G1 and the JIT have parallelism, a deliberate CPU limit choice that avoids harmful throttling, a minimum JDK that reads cgroup v2 correctly, and dashboards for throttling, RSS against limit and OOM kills that keep the rules honest.
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.
A good policy sets memory request equal to limit, sizes the heap with MaxRAMPercentage from a standard default (for example 70 percent) with documented, measured exceptions, gives services at least two CPUs so G1 and the JIT have parallelism, and makes a deliberate choice about CPU limits: many teams use requests only, or generous limits, and watch throttling. It also sets a minimum JDK version that handles cgroup v2 correctly, and provides dashboards for CFS throttling, RSS against the limit and OOMKilled counts so the rules can be checked against reality.
Why this question is asked
Without a policy, every team invents its own pod sizes, and the results are predictable: some services waste memory, some get OOM killed, and some suffer mysterious latency spikes from CPU throttling. A written policy turns lessons learned in incidents into defaults that every new service gets for free. Interviewers ask this design question to see whether we understand how Kubernetes resources and the JVM interact, and whether each rule has a reason we can defend. In this answer, we will write the memory rule, the heap rule, the CPU minimum, the CPU limit decision, the JDK minimum and the monitoring that keeps the policy honest.
1A Quick Recap: Requests, Limits and What the JVM Sees
In Kubernetes, a request is what the scheduler reserves for a pod when placing it on a node, and a limit is the maximum it may use. For memory the limit is hard: exceed it and the kernel kills a process in the container, reported as OOMKilled. For CPU the limit is enforced by the Completely Fair Scheduler as a quota per 100 ms period; exceed it and the container is throttled, paused until the next period. A modern JVM reads the limits, not the requests, from cgroups: the memory limit to size its heap, and the CPU limit to compute availableProcessors().
Those two facts drive the policy. Memory must be predictable because the JVM sizes itself once at startup. CPU limits change how many threads the JVM creates and when they are paused.
2Rule 1: Memory Request Equals Memory Limit
The policy sets memory request and limit to the same value, for example both 2 GiB. The reason is that memory cannot be compressed or shared back easily. If the request is 1 GiB and the limit is 2 GiB, the scheduler packs pods as if each used 1 GiB, but each JVM sizes its heap from the 2 GiB limit and, under load, grows toward it. When several pods on a node grow at once, the node runs out of memory and the kernel starts killing pods, often ones that did nothing wrong.
With request equal to limit, the scheduler's picture is honest, the pod gets the Guaranteed memory it was promised, and the JVM sees a stable number to size from. The cost is less overcommitment, which we accept in exchange for no surprise kills.
3Rule 2: Heap as a Percentage, With Measured Exceptions
The heap is set with -XX:MaxRAMPercentage, not -Xmx, so it follows the pod size. The standard default comes from measuring typical services: a Spring Boot service usually needs 300 to 600 MB of non-heap memory for metaspace, code cache, thread stacks, GC structures and direct buffers. At 2 GiB, 70 percent leaves about 600 MB for that, which fits most services.
Exceptions are allowed but must carry a measurement, usually from Native Memory Tracking and RSS under load, so the next person can tell whether the exception still applies. Because the percentage of a small pod leaves very little absolute headroom, the policy also sets a minimum pod memory, for example 1 GiB, and recommends a lower percentage for small pods.
4Rule 3: At Least Two CPUs Where Possible
A JVM with one CPU is a poor fit for server work. With fewer than two processors, or less than roughly 1792 MB of memory, ergonomics does not treat the machine as server class and selects the Serial collector, which stops the application and collects with one thread. JIT compilation and GC threads compete with request threads for the single CPU, and the ForkJoinPool common pool has just one worker. So the policy gives services at least two CPUs, which also makes the service behave like production in every environment.
Very small services can run on less, but then the policy asks for explicit choices, such as a named collector, rather than letting ergonomics silently pick something different in each environment.
5Rule 4: A Deliberate Decision on CPU Limits
CPU limits are the most debated rule. A tight limit causes CFS throttling: a JVM that runs many threads at once, such as during a parallel GC or JIT warmup at startup, burns its quota in a few milliseconds and is then paused for the rest of the period. That turns a 20 ms GC pause into 100 ms or more. Many organisations therefore set CPU requests but no limits, or limits well above requests, so a service can burst during GC and startup while the request still guarantees its fair share.
| Option | Benefit | Cost |
|---|---|---|
| Request = limit (tight) | predictable, strict isolation | throttling during GC and startup |
| Generous limit (2-4x request) | bursts absorbed, still bounded | noisy neighbour risk on full nodes |
| Request only, no limit | no throttling, best latency | JVM may see node CPUs; set ActiveProcessorCount |
| Policy default | generous limit, throttling monitored | review services with high throttling |
Notice the catch with no limit at all: without a quota, the JVM computes availableProcessors() from the CPUs it can run on, which may be the whole node, so the policy then sets -XX:ActiveProcessorCount to a sensible value such as the request rounded up. Whichever option the organisation picks, the policy states it and the reason, and throttling is measured rather than assumed.
6Rules 5 and 6: A Minimum JDK and the Dashboards
The policy names a minimum JDK release per vendor that supports cgroup v2 correctly, because a runtime that cannot read the limits ignores every other rule in this policy. CI enforces it by rejecting images below the minimum, and the platform team updates the minimum when the infrastructure changes.
Finally, a policy is only as good as its feedback. Every service gets a standard dashboard with three panels: CPU throttling (throttled periods as a share of all periods), RSS against the memory limit, and OOMKilled restarts. Alerts fire when throttling exceeds, say, 10 percent of periods during peak, or RSS stays above 90 percent of the limit. These panels show which rules need exceptions and, over time, whether the defaults themselves need to change.
7Follow-up questions
Why not let the vertical pod autoscaler set memory automatically?
Because the JVM fixes its heap at startup and often keeps committed memory, so usage-based recommendations are skewed, and each change restarts pods. VPA recommendations are useful input for the policy's numbers, but automatic changes to JVM pods tend to cause surprises.
Should InitialRAMPercentage equal MaxRAMPercentage?
For services in pods with request equal to limit, usually yes. The memory is reserved anyway, and a fixed heap avoids resize work and makes RSS reach its real level early, so problems show up in the first minutes rather than after days.
How do we handle services that need more than the default headroom?
Through the documented exception: a lower MaxRAMPercentage with a link to the measurement. The dashboards help find them, since such services show RSS near the limit while heap usage looks moderate.
8Conclusion
In this answer, we wrote a container sizing policy for JVM services: memory request equal to limit so the scheduler and the JVM see the same stable number, heap through MaxRAMPercentage with a measured default and documented exceptions, at least two CPUs so G1 and the JIT have parallelism, a deliberate CPU limit choice that avoids harmful throttling, a minimum JDK that reads cgroup v2 correctly, and dashboards for throttling, RSS against limit and OOM kills that keep the rules honest.
Open with
I'd write six rules, each with its reason. One, memory request equals limit. Memory isn't compressible, and the JVM sizes its heap from the limit, so if the request is lower the scheduler overpacks nodes and pods get OOM killed when they grow together.
Then, if they want more
Two, the heap is MaxRAMPercentage, not Xmx, with a default like 70 percent, because typical Spring services need three to six hundred megabytes of non-heap memory. Exceptions are fine but must cite a measurement, like Native Memory Tracking and RSS under load, and there's a minimum pod size because small pods leave too little absolute headroom.
Three, at least two CPUs, because below that ergonomics picks the Serial collector and GC, JIT and requests fight over one core. Four, a deliberate CPU limit decision.
Tight limits cause CFS throttling, where many GC threads burn the quota in a few milliseconds and then the whole container waits for the next period, so I'd default to generous limits or requests only, and when there's no limit, set ActiveProcessorCount so the JVM doesn't size itself for the whole node.
Five, a minimum JDK per vendor that reads cgroup v2, enforced in CI. Six, standard dashboards for throttling, RSS against the limit and OOMKilled counts, with alerts, so we can see where the rules don't fit and adjust them.
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 "Write the container sizing policy for JVM services in your company: CPU requests and...": https://scrivio-interview-library.netlify.app/java/jvm-tuning/3320d289c6/