Sub topic 1
JIT on the JVM versus ahead-of-time native executables
Prerequisites
This chapter assumes you know the following:
- Java 17 or later and basic Spring Boot (beans, auto-configuration, application properties).
- How to build a Spring Boot project with Maven or Gradle.
- Basic container concepts, such as images and running a container with Docker.
- What Java reflection and dynamic proxies are, at a conceptual level.
You do not need GraalVM installed to read this chapter. Later chapters cover setup and builds.
How a JVM runs your code
A normal Spring Boot application is packaged as a JAR file that holds bytecode. Bytecode is the portable instruction format that the Java compiler produces. When you run java -jar, the Java Virtual Machine (JVM) does the following:
- Starts up and loads classes from the JAR as they are needed.
- Interprets the bytecode at first.
- Watches which methods run often ("hot" methods).
- Compiles the hot methods to machine code with the just-in-time (JIT) compiler.
The JIT compiler uses data it collects while the program runs. It can inline calls and remove branches that never happen. After a warm-up period, this often gives very fast steady-state performance.
The cost is paid at startup and in memory. The JVM must load and verify classes, run the interpreter, and keep the JIT compiler and its profiling data in memory. Spring adds more work at startup. It scans for components, evaluates conditions, and creates bean definitions, often through reflection.
How a native executable runs your code
A native executable is a platform-specific program, for example for Linux x64. It contains machine code and does not need a JVM installed. GraalVM's native-image tool produces it ahead of time (AOT). Ahead-of-time means the compilation happens at build time, before the program ever runs.
The tool analyzes your application and its libraries and compiles everything reachable into one executable. A small runtime called Substrate VM is included. It provides garbage collection and thread support. There is no bytecode interpreter and no JIT compiler.
Comparing the two models
| Aspect | JVM with JIT | Native executable |
|---|---|---|
| Startup time | Seconds for a typical Spring Boot app | Often tens of milliseconds |
| Memory use | Higher, because of JIT and class metadata | Often noticeably lower |
| Peak throughput | Usually higher after warm-up | Often lower or equal, without runtime profiling |
| Build time | Fast | Slow, and needs much memory |
| Portability | Same JAR runs on any OS with a JVM | One executable per OS and CPU |
| Dynamic features | Fully supported | Restricted |
The numbers depend on the application, so measure your own. The pattern is what matters. Native images shift work from run time to build time.
Why dynamic features are restricted
A JVM can load any class at run time, even one chosen by a string. A native executable cannot, because the compiler decides what goes in the file during the build. Code that is not reachable by the analysis is left out. Reflection, resource loading, and dynamic proxies are the usual trouble spots, because the analysis cannot always see them.
This restriction is called the closed-world assumption, and the next chapter covers it. Spring's AOT processing exists to reduce the problem. It replaces much of Spring's reflection-driven startup work with generated code.
Common mistakes
- Expecting a native build to be faster at peak load. It usually starts faster and uses less memory, but it may not beat a warmed-up JVM.
- Assuming a JAR that works on the JVM will work as a native executable. Tests that pass on the JVM can still fail in native mode.
- Building on one OS and running on another. The executable is tied to the platform it was built for.
Exercise
A service must scale from zero instances and answer its first request within a fraction of a second. Another service runs for weeks and handles constant heavy load. Which execution model suits each one best, and why?
Show answer
The scale-from-zero service suits a native executable, because it starts in milliseconds and uses little memory. The long-running service suits the JVM with JIT, because after warm-up the JIT compiler can use runtime profiling to reach high peak throughput, and the startup cost is paid only once.