Quick revision
The short answer
The native image was compiled ahead of time without run-time profiles, so its hot paths are not optimised as aggressively as the JIT optimises them after warmup, and GraalVM Community native images use the Serial GC by default, which stops all threads with one collector thread and can cause longer pauses under heavy allocation. Oracle GraalVM's profile-guided optimisation, and G1 where available, can close part of the gap. The options are to keep native only for bursty, short-lived instances, run the JVM for steady load, or use the JDK AOT cache for faster JVM startup, and to decide with load tests of both builds under the real traffic shape.
What to remember
In this answer, we saw that a native image loses to a warmed JVM under sustained load because ahead-of-time compilation has no run-time profiles and because GraalVM Community's default Serial GC stops all threads for each collection. Profile-guided optimisation and G1 in Oracle GraalVM, plus heap sizing, narrow the gap. Otherwise we keep native for bursty, short-lived instances, run the JVM for steady load, or speed up the JVM with the AOT cache, and we decide with load tests that compare both builds under the real traffic shape.
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.
The native image was compiled ahead of time without run-time profiles, so its hot paths are not optimised as aggressively as the JIT optimises them after warmup, and GraalVM Community native images use the Serial GC by default, which stops all threads with one collector thread and can cause longer pauses under heavy allocation. Oracle GraalVM's profile-guided optimisation, and G1 where available, can close part of the gap. The options are to keep native only for bursty, short-lived instances, run the JVM for steady load, or use the JDK AOT cache for faster JVM startup, and to decide with load tests of both builds under the real traffic shape.
Why this question is asked
Teams often adopt native images for one reason, fast scale-out, and then discover a cost they did not measure: under sustained load the native service is slower than the JVM one. This is not a bug; it is the other side of the same trade. Interviewers ask this question to see whether we can explain the mechanism and turn it into a decision rather than a debate. In this answer, we will see why the JIT wins on sustained load, why the native GC adds to p99, what can be tuned, what the architectural options are, and how a load test settles it.
1Why the JIT Wins Under Sustained Load
On the JVM, code starts interpreted, and the JIT compiler (C2, or Graal JIT in GraalVM's JVM mode) compiles hot methods using profiles gathered while the program runs: which branches are taken, which concrete classes appear at each virtual call, how big loops are. With that knowledge it inlines deeply, removes checks that never fail and lays out code for the common path. If the behaviour changes, it can deoptimise and compile again. After a few minutes of steady load, the JVM is running code tuned to this exact workload.
A native image is compiled before the program ever runs. Without profiles, the ahead-of-time compiler has to assume any branch may be taken and any implementation may appear at a call site, so it inlines less and keeps more general code. For a startup-dominated workload this does not matter. For a service running at 3,000 requests per second for hours, a few percent more CPU per request becomes lower throughput per core and, near saturation, higher p99 latency.
| Metric | JVM, warmed | Native, Community | Native, Oracle + PGO + G1 |
|---|---|---|---|
| Startup | 3.4 s | 0.09 s | 0.10 s |
| Throughput per core | 1,000 req/s | 780 req/s | 940 req/s |
| p99 at 70% load | 38 ms | 61 ms | 42 ms |
| Memory (RSS) | 420 MB | 190 MB | 210 MB |
2Why the Native GC Adds to p99
The second cause is garbage collection. A native image built with GraalVM Community uses the Serial GC by default, a collector that runs on one thread and stops every application thread while it works. On the JVM, the same service most likely ran on G1, which does most of its marking concurrently and uses several threads for its pauses. Under heavy allocation, Serial collections come more often and each pause is longer, and since every request in flight during a pause waits, the pauses show up directly in p99.
Which collectors are available depends on the distribution and platform: Community offers Serial and Epsilon, while Oracle GraalVM adds G1 for native images on Linux, chosen at build time with --gc=G1. Heap size also matters: a native image may run with a small default maximum heap or one derived from available memory, so setting -Xmx explicitly at run time, and checking the GC log, is worth doing; check your version's defaults.
Notice that the GC log flags differ from the JVM's unified logging; native images support a smaller set of options, so check your version's documentation.
3What Can Be Tuned
Before changing the architecture, three tuning steps are worth trying. Profile-guided optimisation in Oracle GraalVM: we build an instrumented image, run it under representative load to collect a profile, and rebuild with it, which gives the ahead-of-time compiler much of the information the JIT would have. A better collector where available, G1 in Oracle GraalVM on Linux. And heap sizing: a larger heap reduces collection frequency, at the cost of some of the memory saving that motivated native in the first place.
These steps usually narrow the gap considerably, but rarely make the native build clearly better than a warmed JVM on sustained throughput. They also depend on the Oracle GraalVM distribution, which has its own licence terms to check.
4The Architectural Options
If tuning is not enough, we choose by traffic shape. Native for bursty, short-lived instances: scale-out pods that live for minutes during spikes, scale-to-zero services and jobs, where fast startup is the whole point and sustained throughput hardly matters. JVM for steady load: the baseline replicas that run all day, where JIT performance and G1 or ZGC pay off. Some teams even run both builds of the same service, a JVM baseline plus native burst capacity, though that doubles build and test work and is only worth it at scale.
The third option is to keep the JVM everywhere and speed up its startup instead. Class data sharing, Spring AOT on the JVM and the JDK AOT cache (JDK 24 and later, with method profiles from a training run in JDK 25) cut startup substantially while keeping the JIT and every collector. If scale-out time was the only reason for native, this often meets the goal with fewer trade-offs, and CRaC can go further where the operations team can support it.
- Load test both buildsreal traffic shape, same CPU and memory
- Native within SLO at peak load?YesKeep native, watch p99NoTry PGO, G1, heap sizing
- Gap closed after tuning?YesKeep tuned native buildNoChoose by traffic shape
- Mostly steady load?YesJVM plus AOT cache or CDSNoNative for bursts, JVM for baseline
4 steps in order. Play them, or step through them one at a time.
5Deciding With Load Tests
The debate ends with a fair load test. Both builds run with the same CPU and memory limits, the JVM version is warmed up first (or the test is long enough that warmup is a small part), and traffic follows the real shape: the same mix of endpoints, payload sizes and concurrency, at baseline, at peak and during a ramp. We record throughput per core, p50 and p99 latency, GC pause times, memory and, for the ramp, how quickly new instances become useful. The comparison often shows that native wins the ramp and loses the plateau, which directly supports the split by traffic shape.
6Follow-up questions
Is the native image slower on every request?
Not necessarily. At low load, response times can be similar or even better, since there is no warmup. The difference shows under sustained load, where less optimised code uses more CPU per request and Serial GC pauses add up, which is exactly where p99 and throughput are measured.
Would GraalVM's JIT mode help?
Running the application on GraalVM as a normal JVM uses the Graal JIT compiler, which has profiles, so it behaves like any warmed JVM with its own optimisation strengths. It gives up native startup, so it is a JVM option, not a native one.
How should autoscaling change if we keep native for bursts only?
The baseline Deployment runs on the JVM with a minimum replica count for steady load, and a second, native Deployment behind the same Service scales on demand. Both builds must be tested and released together, which is the main cost of this design.
7Conclusion
In this answer, we saw that a native image loses to a warmed JVM under sustained load because ahead-of-time compilation has no run-time profiles and because GraalVM Community's default Serial GC stops all threads for each collection. Profile-guided optimisation and G1 in Oracle GraalVM, plus heap sizing, narrow the gap. Otherwise we keep native for bursty, short-lived instances, run the JVM for steady load, or speed up the JVM with the AOT cache, and we decide with load tests that compare both builds under the real traffic shape.
Open with
This is the expected trade-off rather than a bug. The JIT compiles hot code using runtime profiles, so after warmup it inlines aggressively and optimises for the actual branches and types, and it can recompile if behaviour changes.
Then, if they want more
A native image is compiled ahead of time with no profile, so its code is more general and costs more CPU per request, which under sustained load means lower throughput per core and higher p99 near saturation.
The second factor is GC: GraalVM Community native images use the Serial collector by default, single-threaded and stop-the-world, so under heavy allocation the pauses are longer than with G1 on the JVM and they land directly in p99. Other collectors depend on the distribution; Oracle GraalVM has G1 on Linux.
To narrow the gap I'd try Oracle GraalVM's profile-guided optimisation, G1 where available, and an explicit, larger heap. If that's not enough, I'd choose by traffic shape: native for bursty, short-lived scale-out instances or scale-to-zero, JVM for the steady baseline, or keep the JVM everywhere and cut startup with CDS, Spring AOT and the JDK AOT cache.
And I'd settle it with a load test of both builds with the same limits and the real traffic shape, looking at throughput per core, p99, GC pauses and how fast new instances become useful.
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, Spring Boot reference
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 "A team runs a Spring Boot service as a native image for faster scale-out, but under...": https://scrivio-interview-library.netlify.app/spring-boot/native-and-aot/6b7ab56b3c/