Sub topic 10
Startup, footprint, peak throughput and build time
This chapter compares a native executable with the same application on a regular JVM along four axes: startup time, memory footprint, peak throughput and build time. It assumes you can build a native image with native-image or a Maven/Gradle plugin, and that you know roughly what the JIT compiler does. You do not need to have read the chapters on metadata or class initialization. You do need to know that a native image is compiled ahead of time under the closed-world assumption.
Why the numbers differ
A JVM starts by loading and verifying classes, interpreting bytecode, and profiling it. Hot methods are then compiled by the JIT compiler, which uses the profile it has gathered. Native Image does most of that work at build time. The result is a binary whose code is already machine code and whose initial heap is already laid out.
That design moves cost from run time to build time, and it removes information the JIT would have had at run time. Each of the four axes follows from one of those two effects.
| Axis | Native image | JVM | Main reason |
|---|---|---|---|
| Startup | Usually tens of milliseconds | Hundreds of milliseconds to seconds | No class loading, verification, interpretation or warm-up |
| Footprint | Lower resident memory | Higher resident memory | No JIT compiler, no loaded class metadata, only reachable code |
| Peak throughput | Often equal or lower | Often higher on long runs | The JIT uses live profiles; AOT code does not |
| Build time | Minutes and several GB of RAM | Seconds | Whole-program analysis and compilation |
Treat the table as a set of tendencies. The size of each gap depends on the application, the GC, the GraalVM edition and the hardware, so measure your own workload.
Startup time
What a native image skips
On a JVM, a typical framework application loads thousands of classes at startup. Each class is read, parsed and verified, and static initializers run. Framework code often scans the classpath and builds objects through reflection. The interpreter then runs this code slowly until the JIT compiles it.
A native image avoids most of this:
- Machine code is already in the executable.
- Classes initialized at build time have their state stored in the image heap, which the executable maps at start.
- No bytecode is interpreted, so there is no warm-up phase for startup code.
Measuring it
Measure time to first useful result, not only time to main. For a web service, that means time until the first successful HTTP response. A simple check for command-line programs is:
javac Hello.java
native-image Hello
time java Hello
time ./helloRun each command several times and discard the first run, because the operating system caches files on the first read. Report the median.
Limits of the startup advantage
- A program that runs for a long time does not care about a 1-second startup difference.
- Work moved to build time only helps if it is really done at build time. If your code initializes classes at run time and does heavy work in static initializers, startup stays slow.
- Startup is not the only cost. A JVM that starts in 1.5 seconds and is then fast is better for a service that runs for weeks than a native image that starts in 30 milliseconds and is slower afterward. The value depends on how often you start.
Startup matters most for short-lived processes: command-line tools, serverless functions, and scale-to-zero services that start per request. It also matters for fast autoscaling, where new instances must become ready quickly.
Memory footprint
Where the savings come from
A running JVM holds more than your objects. It keeps:
- the loaded class metadata,
- the bytecode and the compiled code produced by the JIT,
- the JIT compiler's own data structures and threads,
- profiling data.
A native image has none of these at run time. It contains only the reachable code, and its heap contains only your objects and the image heap. For small services this can reduce resident set size (RSS) by a large factor.
Measuring RSS
Measure resident memory, not just the Java heap. On Linux:
/usr/bin/time -v ./app 2>&1 | grep "Maximum resident set size"
/usr/bin/time -v java -jar app.jar 2>&1 | grep "Maximum resident set size"Compare under the same load. Idle RSS says little, because a JVM that has not yet warmed up looks small and a native image has a fixed baseline.
Heap sizing differs
A JVM picks default heap limits from the machine or container. A native image uses its own defaults. You can fix the limit at run time with the usual options:
./app -Xmx256mYou can also bake defaults into the executable at build time with -R:MaxHeapSize=..., though a run-time flag is easier to change. The GC choice affects footprint as well. This is covered in the garbage collection chapter, but one point matters here: the default Serial GC has a small footprint and short pauses on small heaps. With big heaps and many threads, it can pause longer than G1. G1 is available on Linux in Oracle GraalVM (--gc=G1), but it costs more memory.
Peak throughput
Why the JVM can win
The JIT compiler sees how the program really runs. It can inline through virtual calls that happen to be monomorphic in practice, remove branches that are never taken, and compile each hot method according to its profile. It can also deoptimize and recompile if its assumptions turn out wrong.
AOT code is compiled once, before the program runs, with no profile. Native Image still performs strong optimizations, and its points-to analysis removes many virtual calls. Even so, it cannot use run-time profiles by default. On a long-running, compute-heavy workload, a warmed-up HotSpot JVM is often faster.
The warm-up effect
The picture changes over time. A JVM starts slow and gets faster. A native image starts fast and stays about the same. This program makes the pattern visible. It times ten rounds of identical work:
public class Warmup {
static long work(int n) {
long sum = 0;
for (int i = 0; i < n; i++) {
sum += Integer.toString(i).hashCode();
}
return sum;
}
public static void main(String[] args) {
long sink = 0;
for (int round = 1; round <= 10; round++) {
long start = System.nanoTime();
sink += work(2_000_000);
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println("round " + round + ": " + ms + " ms");
}
System.out.println("checksum " + sink);
}
}javac Warmup.java
native-image Warmup
java Warmup
./warmupOn the JVM, early rounds are typically slower and later rounds settle at a lower time. The native image times should be flat from round 1. Which one is faster at round 10 depends on your machine and GraalVM edition, so the point of the exercise is the shape of the curve. Printing sink keeps the compiler from removing the loop as dead code.
Closing the gap: PGO and other options
Oracle GraalVM supports profile-guided optimization (PGO). You build an instrumented binary, run it on a representative workload to collect a profile, and rebuild using the profile:
native-image --pgo-instrument Warmup -o warmup-instr
./warmup-instr
native-image --pgo=default.iprof Warmup -o warmup-pgo
./warmup-pgoThe instrumented run writes default.iprof in the current directory. The final build uses it to make the same kinds of decisions the JIT would make, but fixed at build time.
Other settings that affect throughput:
-O3raises the optimization level in Oracle GraalVM. It also raises build time.--gc=G1can improve throughput for allocation-heavy services on large heaps. It increases footprint.-march=nativelets the compiler use CPU features of the build machine. The binary may then fail or crash on older CPUs, so use-march=compatibilityor a named target when you deploy to other hardware.
Build time and build resources
What the build does
The native-image tool runs the points-to analysis over the whole reachable program, initializes classes that are configured for build-time initialization, compiles all reachable methods, and links the result. Because it must analyze the whole program, including libraries and the JDK parts you use, the build is slow compared with javac or a JAR build.
Expect:
- build times from tens of seconds for a small program to several minutes for a large framework application,
- several gigabytes of RAM during the build, often 4 to 16 GB for a framework application,
- a heavy use of CPU cores, which speeds up compilation.
Practical ways to reduce the cost
- Develop and test on the JVM. Build native images only in CI or before releases, and run tests in native mode on a subset.
- Use quick build mode (
-Ob) while iterating. It reduces optimization effort and gives faster builds, but the binary is slower. - Keep dependencies small. Every library that is reachable adds analysis time and binary size.
- Give the build enough memory. If the builder runs out, it slows down or fails. Cap it with
-J-Xmx8gif the machine is shared.
native-image -Ob -jar app.jar -o app-devChoosing between the two deployments
Use these criteria in order:
- Does the process start often or must it scale quickly? That favors native.
- Is memory per instance a cost driver, such as many small containers? That favors native.
- Does the service run for days under heavy CPU load, where each percent of throughput matters? Test the JVM first.
- Is build time or build infrastructure a constraint? That favors the JVM.
- Does your code depend on dynamic features that need heavy metadata work? That adds effort on the native side.
Benchmarking pitfalls
- Comparing different hardware or container limits. Pin CPU and memory for both runs.
- Measuring too soon. Give the JVM time to warm up when you test peak throughput, and measure its warm-up separately.
- Looking at the heap only. Compare RSS, because the JVM's non-heap memory is where much of its extra footprint sits.
- Ignoring tail latency. GC pauses differ between the JVM's default collector and Native Image's Serial GC. Record p99 latency, not only the mean.
- Using one run. Repeat each measurement and report the median and the spread.
Exercise
A team runs a service that handles requests for weeks at a time on large, busy machines. A native image of the service starts in 40 ms, but under steady load its throughput is 15% below the JVM version. Name two options they have for the native build, and say what each trades off.
Show answer
They can build with profile-guided optimization (--pgo-instrument, run a representative workload, then --pgo=default.iprof), which lets the compiler use run-time profile data at the cost of an extra build step and a profile that may go stale. They can also use -O3 or the G1 garbage collector (--gc=G1), which can raise throughput at the cost of longer builds or higher memory use. If none of these closes the gap, the fast startup is worth little for a service that rarely restarts, so staying on the JVM is a reasonable decision.
Exercise
You measure the Java heap of a JVM service and of its native image, and the two numbers look similar. Why can the resident set size of the processes still differ a lot, and which command do you use to compare it?
Show answer
The JVM process also holds class metadata, JIT-compiled code, JIT compiler data and threads, and profiling data outside the Java heap. A native image has none of these at run time. Compare the whole process with /usr/bin/time -v on Linux and read "Maximum resident set size", under the same load for both.