Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Your cloud bill shows JVM pods requesting 4 GiB of memory while median RSS is 1.5 GiB. How would you run a right-sizing program across 500 services without causing OOM incidents?

We treat it as a data-driven program, not a mass edit: collect RSS, heap after GC, GC CPU and OOMKilled events for every service over several weeks, rank services by wasted memory and risk, move everyone to a percentage-based heap so the heap follows the container limit, and then lower limits in small steps with canaries and automatic rollback. Owners get the data and the recommendation, the platform team tracks progress centrally, and tools like the vertical pod autoscaler are treated as advice rather than allowed to change JVM pods on their own.

Background to this question

A 4 GiB request against a 1.5 GiB median RSS means that well over half of the memory we pay for is idle. Across 500 services that can be thousands of GiB and a large monthly bill. The tempting reaction is to cut every request in half, and that is how organisations cause a week of OOM incidents. This question tests whether we can run a change across many teams safely: measure first, change gradually, and make rollback automatic. In this answer, we will look at why the median is the wrong number, what to collect, how to rank services, why the heap must be expressed as a percentage, how to roll out changes, how to work with owners, and where autoscaler recommendations fit.

1Why the Median RSS Is Not the Number to Size For

RSS, the resident set size, is the physical memory a process uses, and it is what the container memory limit is compared against. A median of 1.5 GiB tells us what a typical pod looks like at a typical moment. But pods are not killed at typical moments. They are killed at the peak: during startup and warmup, during a nightly batch, when another zone fails and traffic doubles, or during a deployment when caches are rebuilt.

There is also a JVM detail that makes RSS misleading in both directions. A JVM that has grown its heap usually keeps that memory committed even after the load drops, so RSS shows where the heap once grew to rather than what the service needs now. And a JVM with a large -Xmx that has never been pushed may show low RSS today and still grow toward its maximum next month. So we need to know each service's configured heap and its heap after GC, not only its RSS.

2Collecting the Evidence Over Weeks

The first phase only measures. For every service we collect, per pod, the p99 and maximum RSS, the heap occupancy after GC (the live set), the configured maximum heap, GC CPU time, and every OOMKilled event or restart, together with deployments and traffic peaks. We collect for at least two to four weeks so the data includes month-end jobs, releases and at least one incident or failover.

Most of this comes from existing sources. Kubernetes metrics give RSS (container_memory_working_set_bytes and RSS from cAdvisor) and OOMKilled reasons; Micrometer or JMX gives heap after GC and GC pause time; the deploy system gives the release timeline. The output is one table with a row per service and a suggested limit based on the observed peak plus a margin, for example peak RSS times 1.2 with a floor.

3Ranking Services by Savings and Risk

We do not change 500 services at once. We rank them by two scores. Savings is the wasted memory, requested minus suggested, multiplied by the replica count. Risk comes from how close the peak is to the current limit, whether the service has had OOM events, how spiky its memory is, and how critical it is. A service with 40 replicas each wasting 2.5 GiB is worth far more than one with 2 replicas wasting 1 GiB.

The first wave takes the services with large savings and low risk: stable memory, no OOM history, non-critical paths. They build confidence and usually deliver most of the money. In a typical fleet a small share of the services holds most of the waste, so the first 50 services can easily deliver more than half of the total savings.

Example ranking from the collected data
ServiceRequest / peak RSSReplicasSavingWave
search-api4 GiB / 1.6 GiB40about 80 GiB1 (stable, no OOMs)
pricing4 GiB / 2.1 GiB24about 30 GiB1
billing-batch4 GiB / 3.5 GiB6small, spikylater or never
checkout4 GiB / 2.0 GiB30about 40 GiB2 (critical path)
Savings and risk together decide the order; high-risk services wait until the process is proven.

4Making the Heap Follow the Container Limit

Before we can safely lower a limit, the heap setting must be tied to it. Many services hard-code -Xmx3g. If we cut such a pod's limit from 4 GiB to 2.5 GiB, the heap can still grow to 3 GiB and the kernel kills the pod. So the program standardises the heap as a percentage of the container limit, set through the base image.

Properties
# before: the heap ignores the container limit
JAVA_TOOL_OPTIONS="-Xmx3g" # limit 2.5 GiB -> heap may reach 3 GiB -> OOMKilled
# after: the heap is a share of whatever limit the pod has
JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=70 -XX:InitialRAMPercentage=70"
# limit 4 GiB -> max heap about 2.8 GiB
# limit 2.5 GiB -> max heap about 1.75 GiB, non-heap keeps about 750 MB

Now changing one number, the memory limit, changes heap and headroom together. The percentage itself comes from measurement: a service with a lot of native memory, such as heavy Netty direct buffers, may need 60 percent; a simple one may run at 75 percent. Exceptions are documented per service, which also gives the program a clear list of services that need extra care.

5Rolling Out in Steps With Automatic Rollback

Each change is a normal deployment with guard rails. We lower the limit in steps, for example from 4 GiB to 3 GiB and then later to 2.5 GiB, never straight to the final number. Each step starts on a canary, one pod or a small percentage of traffic, for a period that covers a daily peak. The canary is compared against the baseline pods on GC CPU, p99 latency, restarts and OOMKilled events.

Step throughOne right-sizing step for one service
  1. Pick next stepe.g. 4 GiB -> 3 GiB, heap as a percentage
  2. Deploy to a canarycovers at least one daily peak
  3. GC CPU, p99 or OOM restarts worse?
    YesRoll back automatically, flag the serviceNoRoll out to all pods
  4. Watch for a weekinclude deploys and batch windows
  5. Reached the target?
    YesClose the service in the trackerNoPlan the next step

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

The rollback must be automatic, because a slow memory problem may only appear at 3 a.m. during a batch job. A simple rule such as any OOMKilled event or a 20 percent rise in GC CPU restores the previous limit and flags the service for analysis keeps the program safe even when nobody is watching.

6Working With Owners and Using Autoscaler Advice

Service teams own their services, so the platform team should not silently change their memory. A good model is to give each owner a report with their data, the recommended limit and the expected saving, provide the tooling that makes the change a one-line pull request, and track progress centrally on a dashboard of savings by team. Central tracking matters because without it the program stalls after the enthusiastic first teams.

The Kubernetes vertical pod autoscaler (VPA) is useful as an input. Its recommendations are based on observed usage and give a second opinion. But we should not let it change JVM pods automatically. The JVM decides its heap size once at startup, it often keeps committed heap memory instead of returning it, and VPA applies changes by restarting pods. So VPA would size from inflated RSS, and every adjustment would be a restart. Its numbers belong in the owner report, next to our own measurements.

7Follow-up questions

What if a service genuinely needs its 4 GiB at peak?

Then it stays, and the data says why. The goal is not to cut every service but to remove waste. A service whose peak RSS is 3.5 GiB is correctly sized, and the report should show that so nobody tries again next quarter.

Should the memory request equal the limit?

For JVM services it usually should. The JVM sizes its heap from the limit, and memory is not compressible: if a node overcommits memory and pods grow toward their limits, the kernel kills processes. Request equal to limit gives the scheduler an honest number and the pod a guaranteed amount.

How do we stop the waste from coming back?

Make the right size the default: the base image sets a percentage-based heap, new services start from a template with a measured limit, and a monthly report flags services whose request is far above their p99 RSS. Waste returns when nobody looks, so the report keeps looking.

8Conclusion

In this answer, we saw why the median RSS is not a sizing target and collected weeks of RSS, heap after GC, GC CPU and OOM data per service. We ranked services by savings and risk, tied the heap to the container limit with MaxRAMPercentage, lowered limits in small canaried steps with automatic rollback, gave owners the data while tracking progress centrally, and used VPA recommendations as advice rather than automation.

What to say in the interviewAbout 90 seconds aloud

Open with

I would run it as a measured program, not a mass edit. First, a few weeks of data per service: peak and p99 RSS, heap after GC, configured max heap, GC CPU and every OOMKilled event, lined up against deployments and traffic peaks, because pods die at the peak, not at the median.

Then, if they want more

From that I compute a suggested limit, roughly peak RSS plus a margin, and rank services by savings times replicas against risk, so the first wave is big, stable, non-critical services.

Before touching any limit I standardise the heap as MaxRAMPercentage in the base image, because a hard-coded Xmx3g in a pod I cut to 2.5 gigabytes is a guaranteed OOM kill.

Then each change goes in steps, 4 to 3 gigabytes before 2.5, on a canary that covers a daily peak, with automatic rollback if GC CPU, p99 latency or OOM restarts get worse. Owners get a report with their numbers and a one-line change to make, and I track savings centrally so the program does not stall.

I would use the vertical pod autoscaler's recommendations as input, but not let it resize JVM pods itself, since the JVM fixes its heap at startup and keeps committed memory, so VPA's view is skewed and every change is a restart.

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 "Your cloud bill shows JVM pods requesting 4 GiB of memory while median RSS is 1.5 GiB....": https://scrivio-interview-library.netlify.app/java/jvm-tuning/948a54f3ab/