Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Your cloud bill for 300 Java services is rising 30 percent per year. What JVM and platform levers would you use to reduce cost?

I would start by measuring cost per request so we work on the services where savings are largest. Then the main levers are right-sizing heap and container memory from real usage, moving to newer JDKs that use memory and GC more efficiently, including compact object headers in JDK 25, moving suitable workloads to ARM instances, and cutting idle replicas with autoscaling and scale-to-zero for rarely used services.

Why this question is asked

A 30 percent yearly increase doubles the bill in under three years, and in a fleet of 300 Java services the waste is rarely in one place. Java services are especially prone to over-provisioning: heaps set once and never revisited, CPU requests sized for startup, and replicas kept for a peak that lasts an hour. Interviewers ask this to see whether we know the JVM and platform levers and can prioritise them with data. In this answer, we will start with measurement, then go through right-sizing, newer JDKs, ARM instances and idle capacity, and finish with how to roll the changes out safely.

1Measure Cost per Request First

Before touching any JVM flag, we need to know where the money goes. Total cost per service is a start, but it hides growth: a service whose cost doubled because its traffic doubled is fine, while one whose cost doubled with flat traffic is wasting money. So the key number is cost per unit of work, for example cost per million requests or per thousand orders.

With 300 services, cost is usually concentrated: often 20 or 30 services account for most of the compute bill. Sorting by total cost and by cost per request tells us where to start, and the same numbers later show whether the changes worked.

2Right-Sizing Heap and Container Memory

Many Java services run with memory settings chosen at creation and never revisited. A container with a 4 GB limit and a heap of 3 GB might use only 1.2 GB of live data after garbage collection. Right-sizing starts from real usage: the heap after GC at peak, the container's total memory usage, and CPU usage at peak. Container memory must cover more than the heap: metaspace, thread stacks, the JIT's code cache, direct buffers and GC structures, so a common pattern is to size the heap as a percentage of the container with -XX:MaxRAMPercentage.

Java
# Before: sized once, never revisited
resources:
requests: { memory: 4Gi, cpu: "2" }
limits: { memory: 4Gi }
JAVA_TOOL_OPTIONS="-Xmx3g"
# measured at peak: heap after GC 1.1 GB, container RSS 1.9 GB, CPU 0.6 cores
# After: based on measurements plus headroom
resources:
requests: { memory: 2560Mi, cpu: "1" }
limits: { memory: 2560Mi }
JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=70" # about 1.75 GB heap
# per pod: 1.5 GiB and 1 core less requested; across 40 pods: 60 GiB and 40 cores

Notice that we keep headroom: the new heap is still well above the 1.1 GB live set, so the collector has room to work. Cutting too close causes more frequent GC, higher CPU and latency, or containers killed for exceeding their memory limit. CPU requests deserve the same review, because many Java services request CPU for their startup burst and then idle at a fraction of it.

3Newer JDKs Do More With Less

Each JDK release improves the garbage collectors, the JIT and the memory footprint, often for free. Services still on JDK 11 or 17 can gain from the work since then, including generational ZGC for low-latency services and continued G1 improvements. JDK 25 also includes compact object headers (JEP 519), which shrink the header every object carries from 12 bytes to 8 bytes on 64-bit JVMs. For heaps full of small objects, that can reduce heap usage noticeably and improve cache efficiency. It is opt-in with -XX:+UseCompactObjectHeaders, so check your version and measure on a few services before adopting it widely.

YAML
# JDK 25: opt in to compact object headers and compare
java -XX:+UseCompactObjectHeaders -XX:MaxRAMPercentage=70 -jar catalog.jar
# compare heap after GC and GC CPU before and after on the same traffic

The upgrade itself costs testing effort, so the JDK move pays off most for the largest services, and it brings security and performance benefits beyond cost.

4ARM Instances and Idle Capacity

ARM-based instances, such as AWS Graviton, often cost less per unit of performance than comparable x86 instances. Java runs well on ARM because the JVM abstracts the hardware, so most services move with a multi-architecture container image. The catch is native dependencies, such as compression or crypto libraries with native code, and performance differences for specific workloads. So we benchmark a service on both before moving it and move the fleet gradually.

Idle capacity is often the biggest waste. Many services run a fixed number of replicas sized for their daily peak, so for 16 hours a day most of them idle. Horizontal autoscaling on CPU or request rate removes replicas when traffic drops. For services that are rarely used, such as internal tools or test environments, scale-to-zero, for example with Knative or KEDA, removes them entirely when there is no traffic; this is where faster startup through the AOT cache or native images helps, because the first request has to wait for a pod to start. Shutting down non-production environments outside working hours is a simple saving too.

Levers for the Java fleet
LeverWhat it savesMain risk
Right-size heap and requestsMemory and CPU requestedOOMKilled or GC pressure if cut too far
Newer JDK, compact headersHeap and GC CPUUpgrade testing effort
ARM instancesPrice per unit of performanceNative libraries, benchmark per service
Autoscaling, scale-to-zeroIdle replicasCold start latency
Cost per requestFocus on the biggest winsNone; it guides the rest
Most fleets find their largest savings in the first and fourth rows.

5Rolling the Changes Out Safely

Cost changes can hurt reliability if rushed, so I would roll them out like any other change: start with the 20 or 30 most expensive services, change one lever at a time, watch latency, error rates, GC time and restarts for a week, then move on. Recommendations based on measured usage can be generated automatically, for example with the Kubernetes Vertical Pod Autoscaler in recommendation mode, and teams apply them in their own manifests. Reporting cost per request every month shows the effect and keeps the work going.

6Follow-up questions

How much can compact object headers save?

It depends on how many small objects the service keeps. Each object saves 4 bytes of header, so a heap with tens of millions of small objects saves hundreds of megabytes, while a heap dominated by large arrays barely changes. The only reliable answer is to measure heap after GC and GC CPU with the flag on and off under the same traffic.

Why not just set very low memory limits to force savings?

Because the JVM needs room beyond the live set. With too little heap, the garbage collector runs constantly, CPU rises and latency suffers, and with too little container memory, the kernel kills the process. Savings must be based on measured usage plus headroom, and reliability metrics must be watched after each change.

Is it worth moving everything to ARM?

Not automatically. Most pure-Java services move easily and save money, but services with native dependencies or specific performance characteristics need testing. A good approach is to build multi-architecture images by default, benchmark the most expensive services first, and move those that show a clear benefit.

7Conclusion

In this answer, we saw how to cut the cost of a 300-service Java fleet. We start by measuring cost per request to find the biggest opportunities, right-size heap and container memory from real usage while keeping headroom, upgrade to newer JDKs and consider compact object headers in JDK 25, move suitable workloads to ARM instances after benchmarking, and remove idle replicas with autoscaling and scale-to-zero. Rolling changes out gradually and watching reliability keeps the savings from turning into incidents.

What to say in the interviewAbout 90 seconds aloud

Open with

First I would measure, because with 300 services the cost is usually concentrated in a few dozen. I would compute cost per service and cost per request or per order, which separates growth from waste, and start with the most expensive services.

Then, if they want more

The biggest lever is usually right-sizing. Many Java services keep the heap and container memory they were created with, so I would compare requests with real usage: heap after GC at peak, container memory and CPU at peak.

Then I size the heap with MaxRAMPercentage, leaving room for metaspace, thread stacks and direct buffers, and reduce CPU requests sized for startup. I keep headroom, because cutting too close causes GC pressure or OOMKilled pods.

Second, newer JDKs: GC and footprint improve with every release, and JDK 25 has compact object headers, which shrink object headers from 12 to 8 bytes and can noticeably reduce heap for services with many small objects. It is opt-in, so I would measure first.

Third, ARM instances like Graviton are often cheaper per unit of performance, and pure Java moves easily, but I would benchmark and watch for native libraries. Fourth, idle capacity: autoscaling instead of fixed replicas sized for peak, scale-to-zero for rarely used services, and switching off non-production environments at night.

I would roll this out one lever at a time while watching latency, errors and restarts, and report cost per request monthly.

About this answer

Last updated
12 November 2026
Versions it names
Java 25, Java 11
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 for 300 Java services is rising 30 percent per year. What JVM and...": https://scrivio-interview-library.netlify.app/spring-boot/production-operations/465436d82f/