Sub topic 2
Warmup and JIT compilation
This chapter assumes you know basic Java and can run a class from the command line. It also assumes you know that the JVM compiles hot code at runtime. Here we look at what that compilation does to timing numbers.
The JVM does not run your code at one speed
When a Java program starts, the JVM does not turn your bytecode into machine code right away. It first runs the bytecode in an interpreter. This is slow, but it starts at once.
While the interpreter runs, the JVM counts how often each method is called and how often each loop jumps back. When a count passes a threshold, the code is "hot". The JIT (just-in-time) compiler then turns it into machine code in a background thread.
HotSpot uses tiered compilation:
- The interpreter runs everything at first and collects profile data.
- C1 compiles quickly and applies light optimizations.
- C2 compiles slowly and applies aggressive optimizations, such as inlining and loop unrolling. It uses the profile gathered earlier.
A loop that is already running can be switched to compiled code in the middle of its run. This is called on-stack replacement (OSR).
The last arrow matters. C2 often bets on what it saw in the profile, for example that only one class ever reaches a call site. If the bet fails, the JVM discards the compiled code (deoptimization) and starts again in the interpreter.
Seeing warmup in practice
This program times the same work in eight rounds. Nothing in it changes between rounds.
public class WarmupDemo {
static long work(int[] data) {
long total = 0;
for (int value : data) {
total += value;
}
return total;
}
public static void main(String[] args) {
int[] data = new int[10_000];
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
long sink = 0;
for (int round = 1; round <= 8; round++) {
long start = System.nanoTime();
for (int i = 0; i < 2_000; i++) {
sink += work(data);
}
long elapsed = System.nanoTime() - start;
System.out.println("round " + round + ": " + elapsed / 1_000 + " us");
}
System.out.println("sink = " + sink);
}
}Run it with java WarmupDemo.java. The early rounds are usually slower than the later ones. The exact numbers depend on your machine and JVM, so run it a few times. To see the compiler at work, add -XX:+PrintCompilation and watch methods appear in the log as they get compiled.
Why this breaks timing measurements
Warmup means the first measurements describe a different program from the one that runs in production. Several effects add to it:
- Compilation level. Early iterations measure the interpreter or C1 code, not C2 code.
- Compiler threads. Compilation competes for CPU while you are measuring.
- Class loading and initialization. These costs fall on the first calls.
- Profile pollution. If a loop first sees one type and later sees another, C2 may deoptimize and recompile.
A single run of a hand-written loop gives you an average over all of these phases. You can't tell which phase your number came from.
The usual fix is a warmup phase. You run the code long enough for compilation to settle, throw those timings away, and only then measure. Even that is not enough on its own. The result depends on the profile that one JVM run happened to collect, so you need to repeat the experiment in fresh JVM processes. JMH automates both parts, and later chapters cover its warmup iterations and forks.
Exercise
Why does running a method in a loop a few times and timing the last run not give a trustworthy measurement of its steady-state speed?
Show answer
The JVM may not have compiled the method with C2 by then. The timing can include interpreter or C1 code, compilation threads competing for CPU, and later deoptimization. It also reflects only one JVM run's profile, so a proper warmup phase and several fresh JVM forks are needed.