Quick revision
The short answer
I would decide with measurements, not opinions: compare requested with used memory, split heap from non-heap, and count idle replicas. Tuning and right-sizing almost always give the largest and cheapest savings first, newer JDKs add footprint reductions such as compact object headers along with security benefits, native images suit small and bursty services, and rewrites in another language carry the most risk and need strong evidence. The work goes in sequence, each step with a savings target and a review before the next.
What to remember
In this answer, we saw how to decide between tuning, newer JDKs, native images and rewrites to reduce JVM memory costs. We started by measuring requested versus used memory, heap versus non-heap and idle replicas, did the cheap and safe right-sizing and tuning first, used JDK upgrades for footprint features like compact object headers along with security benefits, targeted native images at small and bursty services, required strong evidence for any rewrite, and sequenced it all with savings targets and reviews.
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.
I would decide with measurements, not opinions: compare requested with used memory, split heap from non-heap, and count idle replicas. Tuning and right-sizing almost always give the largest and cheapest savings first, newer JDKs add footprint reductions such as compact object headers along with security benefits, native images suit small and bursty services, and rewrites in another language carry the most risk and need strong evidence. The work goes in sequence, each step with a savings target and a review before the next.
Why this question is asked
When memory for JVM containers becomes a large line in the cloud bill, every option gets a champion: the platform team wants tuning, someone has read about GraalVM, and someone else is sure Go would halve the cost. At the senior level, interviewers want to see how we turn that debate into a plan that saves real money without risking the business. In this answer, we will start with measurement, look at what each option really saves and costs, and put them in an order with targets and reviews.
1Start With Measurement
Before choosing any option, we need to know where the memory goes. Three numbers matter most. First, requested versus used memory per service: Kubernetes bills us, in effect, for what pods request, because requests reserve node capacity. If a service requests 4 GiB and its pods use 1.5 GiB at peak, most of the cost is reservation, not use. Second, heap versus non-heap per service, from JVM metrics and Native Memory Tracking, which tells us whether the JVM itself is large or the heap is oversized. Third, idle replicas: services that run ten replicas at night for traffic that two could serve.
In most fleets I have seen, this first look shows the cost is dominated by over-requesting and idle capacity, not by Java being heavy. That shapes everything that follows.
| Category | Share | Lever |
|---|---|---|
| Requested but never used | 40 percent | right-size requests and heap |
| Idle replicas off-peak | 15 percent | autoscaling, scale-down schedules |
| Heap actually used | 30 percent | tuning, newer JDK, code fixes |
| Non-heap JVM memory | 15 percent | thread and buffer caps, newer JDK, native images |
2Tuning and Right-Sizing First
Right-sizing means setting each service's memory request and limit to what it needs at peak plus a margin, and setting the heap as a percentage of that limit. Tuning means fixing the obvious waste: a default 25 percent heap that forces teams to request four times the memory they need, unbounded thread pools, oversized caches, and replicas that never scale down. Horizontal autoscaling on real load and scale-down schedules for internal tools remove idle capacity.
The reason to do this first is the ratio of savings to risk. It needs no code changes and no new technology, it can be done service by service, and it is reversible in minutes. A 20 to 40 percent reduction in requested memory is a common result. Every later option also benefits from it, because we cannot judge whether a native image saves memory if the JVM version was badly oversized to begin with.
3Newer JDKs Reduce Footprint
The second step is upgrading. Each recent JDK release has reduced memory use without code changes. Compact object headers, experimental in JDK 24 and a product feature in JDK 25 turned on with -XX:+UseCompactObjectHeaders, shrink every object header from 12 to 8 bytes on 64-bit JVMs, which can cut heap use by 10 to 20 percent for object-heavy services. G1 has become better at returning unused memory, generational ZGC uses less memory than the older single-generation version, and class data sharing reduces metaspace and startup time when several JVMs share an archive.
Upgrades also bring security patches and longer support, so they have value even without the savings. The cost is testing effort and some framework work, for example moving to Spring Boot versions that support the newer JDK. I would run the upgrade as a platform programme with a pilot set of services and measure the memory change before rolling out.
4Native Images for Small and Bursty Services
A GraalVM native image compiles the application ahead of time into a standalone executable. There is no JIT compiler and no class loading at runtime, so a small Spring Boot service may use 100 to 200 MiB of memory instead of 400 to 600 MiB, and start in a fraction of a second. That suits services that are small, scale to zero, or scale out quickly in bursts.
But there is a catch. Peak throughput is often lower without the JIT's runtime optimizations, unless profile-guided optimization is used. Builds take minutes and several gigabytes of memory, and libraries that rely on reflection or dynamic proxies need extra configuration. So I would target native images at a specific group of services where the profile fits, not the whole fleet.
5Rewrites Need Strong Evidence
Rewriting a hot service in Go or Rust can reduce memory further, but it is by far the most expensive and risky option. We pay for the rewrite itself, we lose years of fixes embedded in the old code, we need people who can maintain the new language, and the platform's shared libraries, security and observability have to be rebuilt for it. The savings must be large enough to pay for all of that, which is rare after the earlier steps.
I would only consider it for a service that is still a large share of the bill after tuning and upgrades, where the profile shows the JVM itself is the dominant cost, and where the team is willing and able to own the new stack. Even then, I would ask for a prototype with real measurements before committing.
6A Sequenced Plan With Targets
Putting it together, the plan has steps in order of cost and risk, each with a savings target and a review before the next one. Moving to the next step is a decision based on what the previous one achieved and what remains.
- Measure fleetrequested vs used, heap vs non-heap, idle replicas
- Right-size and tunetarget: 25 percent fewer requested GiB in two quarters
- Upgrade JDK, enable footprint featurestarget: 10 percent less on upgraded services
- Large small or bursty services remain costly?YesPilot native images for themNoStop and keep tuning
- A hot service still dominates and evidence is strong?YesPrototype a rewrite with measurementsNoNo rewrite
5 steps in order. Play them, or step through them one at a time.
Each step is reviewed against its target before the next begins.
7Follow-up questions
How do you make teams care about memory costs?
Show each team its own cost: requested versus used memory per service on a dashboard with the monthly bill. Give them a simple recommended request based on observed peak, and celebrate the savings. Most over-requesting is caution, not carelessness, so a safe default and a quick rollback path help more than mandates.
Can right-sizing hurt reliability?
It can if done carelessly, for example setting limits to the average rather than the peak. We base new sizes on the peak over several weeks plus a margin, change one service at a time, and watch restarts, OOMKills and latency for a week before moving on.
Why not start with native images since they promise the biggest drop?
Because the drop per service is large but the cost is also large: build changes, compatibility testing and lower peak throughput for some workloads. Tuning reaches every service at almost no risk. Starting with native images would spend the most effort on the smallest part of the bill.
8Conclusion
In this answer, we saw how to decide between tuning, newer JDKs, native images and rewrites to reduce JVM memory costs. We started by measuring requested versus used memory, heap versus non-heap and idle replicas, did the cheap and safe right-sizing and tuning first, used JDK upgrades for footprint features like compact object headers along with security benefits, targeted native images at small and bursty services, required strong evidence for any rewrite, and sequenced it all with savings targets and reviews.
Open with
I would start with measurement, because the debate is usually about technology while the money is somewhere else. For every service I want requested versus actually used memory, heap versus non-heap from JVM metrics, and the number of replicas that sit idle off-peak.
Then, if they want more
In most fleets the biggest waste is over-requesting and idle capacity, not Java itself. So step one is right-sizing and tuning: requests based on observed peaks with a margin, heap as a percentage of the limit, autoscaling and scale-down schedules. It needs no code changes, it is reversible, and 20 to 40 percent savings are common.
Step two is the JDK upgrade. Newer releases reduce footprint, for example compact object headers in JDK 25 shrink every object, and G1 and ZGC have improved, plus we get security fixes. Step three, only for services whose profile fits, small, bursty or scale-to-zero, is a native image pilot, accepting longer builds and some throughput and compatibility trade-offs.
A rewrite in another language is the last resort, for a service that still dominates the bill after all that and has a team ready to own the new stack, and only after a prototype with real numbers. Each step has a savings target, and we review the result before starting the next.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 25
- 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.
- Official documentation
- Kubernetes documentation
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 "The company spends a large share of its cloud budget on memory for JVM containers. As...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/4c305bb763/