Sub topic 3
What GraalVM native-image produces
This chapter assumes you know Java 17 or later, have built Spring Boot projects with Maven or Gradle, and understand the earlier chapters on JIT versus ahead-of-time compilation and the closed-world assumption. Here you will see what the native-image tool takes in and what comes out.
Input and output
native-image is a compiler and linker shipped with GraalVM. It reads compiled Java bytecode (your classes, your dependencies, and the JDK classes they use) and writes one native executable file. Spring Boot's build plugins call it for you, but it helps to run it directly once.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from a native image");
}
}javac Hello.java
native-image Hello
./helloThe tool names the output after the main class, in lowercase. The result is a file you can run without a JVM on the machine.
What happens during the build
The build runs in stages:
- Starting from the entry points (
mainand anything you registered), the tool analyzes which classes, methods and fields are reachable. - It runs static initializers for classes configured to initialize at build time, and saves the resulting objects in an image heap.
- It compiles only the reachable methods to machine code.
- It links that code, the image heap and a runtime into the executable.
What is inside the executable
The executable contains four things:
- Machine code for reachable methods only. Unreachable code is dropped, which is one reason the file and its memory use stay small.
- The image heap, a snapshot of objects created at build time. This is part of why startup is fast, because that work is already done.
- Substrate VM, a small runtime that provides garbage collection, thread support and other services the JVM would normally supply.
- Metadata for the features you registered, such as reflection and resources.
The executable does not contain a bytecode interpreter, a JIT compiler or a class loader that reads new classes at runtime. It cannot load a class that was not known at build time.
Differences from a JVM process
| Aspect | JVM | Native executable |
|---|---|---|
| Startup | Loads classes, warms up the JIT | Starts from a prepared image |
| Peak throughput | JIT optimizes with live profiles | Fixed code, so often lower unless you use profile-guided optimization |
| Memory | Higher baseline | Lower baseline |
| Portability | One jar runs on any platform | One executable per operating system and CPU architecture |
| Dynamic features | Reflection and class loading work freely | Need build-time knowledge or metadata |
The default garbage collector is Serial GC. Other collectors, such as G1, depend on the GraalVM distribution and platform.
Build cost
The analysis and compilation steps are expensive. A small Spring Boot application commonly takes minutes to build and uses several gigabytes of memory. Plan for this in CI and on developer machines, and keep the regular JVM build for fast feedback during development.
Why Spring needs more than the bare tool
Run directly on a Spring Boot fat jar, native-image cannot see how Spring wires beans, because Spring does that through reflection and classpath scanning at runtime. Spring AOT processing runs before native-image and generates plain Java code and hint files that make the wiring visible to the analysis. The next chapters cover that step.
Exercise
A teammate builds a native executable on a Windows laptop and copies it into a Linux container. It fails to start. Why, and what is one fix?
Show answer
A native executable is compiled for the operating system and CPU architecture where it was built, so the Windows binary cannot run on Linux. Build it on Linux, for example in CI or with Cloud Native Buildpacks, which produce a Linux image.
Exercise
Name two things the native executable contains that a plain bytecode jar does not, and one thing it leaves out that a JVM process has.
Show answer
It contains machine code and the image heap (also the Substrate VM runtime). It leaves out the JIT compiler, which a JVM process uses to optimize code while running.