Sub topic 10
Startup, footprint, peak throughput and build time
This chapter assumes you can build a native executable with native-image, know what the JVM and its JIT compiler do, and have read the earlier chapters on the closed-world assumption and runtime memory. It compares a native executable with the same application on a regular JVM along four dimensions: startup, footprint, peak throughput and build time.
The four dimensions
A JVM starts by loading and verifying classes, then interprets bytecode. It compiles hot methods with the JIT compiler while the program runs. A native executable has already done most of that work at build time. It starts from a prepared heap and machine code.
| Dimension | Native executable | JVM |
|---|---|---|
| Startup time | Typically milliseconds | Typically hundreds of milliseconds to seconds |
| Memory footprint | Lower resident memory, no JIT or class metadata | Higher, includes JIT compiler, code cache and metadata |
| Peak throughput | Fixed at build time | Can be higher after warm-up |
| Build time | Minutes, high memory use | Seconds |
These are typical tendencies, not guarantees. Measure your own application.
Startup and footprint
Native Image moves class loading, linking and much initialization to build time. The first request is served at close to normal speed, because there is no warm-up phase. This suits command-line tools, serverless functions and services that scale to zero.
Footprint is lower for three reasons. Unreachable code is removed. There is no JIT compiler running in the process. Class metadata is compact. A small service that needs a few hundred MB on a JVM may run in a fraction of that as a native executable.
You can check both numbers yourself. This script measures wall-clock time to finish for a trivial program and the peak resident memory:
javac Hello.java
native-image Hello
/usr/bin/time -v java Hello 2>&1 | grep -E "Elapsed|Maximum resident"
/usr/bin/time -v ./hello 2>&1 | grep -E "Elapsed|Maximum resident"On macOS, use /usr/bin/time -l instead. For a server, measure the time until the first successful response, not only process start.
Peak throughput
The JIT compiler profiles the running program. It inlines based on observed behavior and recompiles with speculative optimizations. After warm-up, a long-running JVM often beats a default native image on raw throughput. A native image has no run-time profile, so it relies on static heuristics.
Two options narrow the gap:
- Profile-guided optimization (PGO) feeds a recorded run profile into the build. The compiler then optimizes the paths your workload really uses.
- The garbage collector choice also matters. The default Serial GC is small and has low overhead but pauses the whole application during collection.
The PGO workflow has three steps:
native-image --pgo-instrument -jar app.jar -o app-instrumented
./app-instrumented # run a representative workload; writes default.iprof
native-image --pgo=default.iprof -jar app.jar -o appBuild time
A native build runs static analysis over the whole reachable program, then compiles all of it. Expect one to several minutes for a small service and longer for large ones. Builds also need a lot of RAM, often several GB.
Practical ways to manage this:
- Use the JVM for local development and tests. Build native images in CI or before release.
- Use the quick build mode (
-Ob) while iterating, which skips expensive optimizations. Do not benchmark such an image. - Give the builder enough memory. A starved build is slow or fails.
- Keep dependencies small. Fewer reachable classes mean less analysis.
native-image -Ob -jar app.jar -o app-devChoosing between them
Pick a native executable when startup time or memory dominates your costs: serverless functions, CLI tools, short-lived jobs, and densely packed containers. Pick the JVM when the service runs for days, has a large steady load, and needs the best peak throughput, or when your code relies heavily on dynamic features that need extensive metadata.
A common mistake is comparing a cold native image with a warmed JVM, or the reverse. Decide which scenario you care about, then measure that one with a realistic workload.
Exercise
Your team runs a service on a JVM that handles steady heavy traffic for weeks at a time. A colleague proposes switching to native images because they start faster. Which two of the four dimensions weigh against the switch, and what could you try to reduce the throughput gap?
Show answer
Peak throughput and build time weigh against it. After warm-up the JIT can beat a default native image, and a native build is slower and needs more memory. Fast startup matters little for a service that rarely restarts. To reduce the throughput gap, use profile-guided optimization (in Oracle GraalVM) with a representative workload, and consider a different garbage collector.
Exercise
You built an image with -Ob and it shows lower throughput than the JVM. Is this a valid comparison?
Show answer
No. The -Ob mode skips expensive optimizations to speed up the build, so it is meant for development. Rebuild without it, and ideally with PGO, before you benchmark.