Sub topic 1
From bytecode on a JVM to a standalone executable
Prerequisites
This chapter assumes you can compile and run Java programs from the command line and know what a JAR and the classpath are. You should also have a general idea of what the JVM and JIT compilation do. You don't need GraalVM installed yet. The next chapter builds your first native executable.
How a JVM runs your program
javac turns source code into bytecode, a compact instruction set that is the same on every platform. Bytecode can't run directly on a CPU, so the JVM does the following each time you start a program:
- Starts the runtime and loads the core classes.
- Finds, loads and verifies your classes and your dependencies' classes.
- Interprets the bytecode.
- Watches which methods run often and compiles those to machine code with the just-in-time (JIT) compiler.
Step 4 is why long-running Java services are fast. The JIT sees real behavior, such as which branches are taken and which types actually appear at a call site. It can optimize for that behavior, and it can discard the optimization if the behavior changes. The cost is that all of this work happens at run time, on every start.
You can see the warm-up effect with a small program:
public class Warmup {
static long work(int n) {
long sum = 0;
for (int i = 0; i < n; i++) {
sum += i % 7;
}
return sum;
}
public static void main(String[] args) {
for (int round = 1; round <= 5; round++) {
long start = System.nanoTime();
long result = work(50_000_000);
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println("round " + round + ": " + ms + " ms (result " + result + ")");
}
}
}On most machines the first round is slower than the later ones, because the JIT hasn't yet compiled work to optimized machine code. Exact numbers vary by hardware and JVM version.
What ahead-of-time compilation changes
Ahead-of-time (AOT) compilation moves the translation to machine code from run time to build time. GraalVM Native Image is an AOT compiler for Java. It takes your bytecode, your dependencies and the parts of the JDK you use. It produces one executable for a specific operating system and CPU architecture.
The executable doesn't need a JVM installed. It contains your compiled code and a small runtime called Substrate VM, which provides garbage collection, thread scheduling and other services. Nothing is interpreted, and nothing is loaded or verified at startup.
What you gain and what you give up
The trade-off follows from moving work to build time.
| Aspect | JVM with JIT | Native Image |
|---|---|---|
| Startup | Slower, because of class loading and warm-up | Typically milliseconds |
| Memory footprint | Larger, because of the JIT compiler and class metadata | Typically smaller |
| Peak throughput | Often higher, because of profile-based optimization | Can be lower without extra tuning |
| Build time | Fast | Slow, because of whole-program analysis |
| Portability | One JAR runs anywhere | One executable per OS and architecture |
| Dynamic features | Fully supported | Need extra configuration |
The last row matters most for later chapters. The JVM can load any class by name at any time. A native image can't, because the compiler must decide at build time what goes into the executable. This is the closed-world assumption, covered in a later chapter.
Choosing between them
Ask what your deployment needs most:
- If startup time or memory per instance drives your cost or latency, consider a native image.
- If the application runs for a long time and peak throughput matters, the JVM is usually the safer default.
- If the code relies heavily on reflection, dynamic proxies or runtime class generation, expect extra work to make it native-ready.
Exercise
A team runs a batch job that starts, processes a few records for two seconds and exits, thousands of times a day. Another team runs a service that stays up for weeks under constant load. Which deployment model suits each team best, and why?
Show answer
The batch job suits Native Image. It is short-lived, so JVM startup and warm-up are a large share of its total run time, and the executable removes most of that cost. The long-running service suits the JVM. The JIT has weeks to profile and optimize hot code, so the service can reach higher peak throughput, and startup cost is paid only once.