Sub topic 2
Why HotSpot Compiles at Run Time
This chapter assumes you know basic Java syntax, methods, loops, and classes. You should also know that javac turns Java source into JVM bytecode and that the JVM runs that bytecode.
The problem with compiling everything before startup
A Java program can contain thousands of methods, but one run usually uses only a small part of them. Some methods run once during startup. Others run repeatedly inside request handlers, parsers, loops, or collection operations.
Compiling every method into highly optimized native machine code before the program starts would waste time and memory. It could also optimize code based on assumptions that do not match the actual workload.
HotSpot takes a different approach:
- It starts methods in the bytecode interpreter.
- It measures which methods and loops run often.
- It compiles frequently used code into native machine code.
- It uses information collected during execution to improve that compiled code.
This is called just-in-time compilation, or JIT compilation. “Just in time” means that compilation happens while the program is running, shortly before optimized code is needed.
Why interpretation comes first
The interpreter has low startup cost. It can begin executing bytecode without waiting for a compiler to analyze the entire application. While it runs, HotSpot collects execution data such as:
- How many times a method is called.
- How often a loop branches back to its beginning.
- Which concrete classes appear at a call site.
- Which branches are usually taken.
- Which methods are small and frequently called.
This data is called a profile. The profile represents the behavior of the current run, rather than an assumption about every possible run.
For a short command-line tool, interpretation may be sufficient because the program exits quickly. For a server that handles millions of requests, repeatedly interpreting the same hot methods would spend CPU time decoding bytecode. Compiling those methods can repay the compilation cost many times over.
The cost and benefit
JIT compilation is useful when code runs often enough to recover the cost of compiling it.
| Execution strategy | Main benefit | Main cost |
|---|---|---|
| Interpretation | Fast startup | More CPU work per bytecode operation |
| JIT compilation | Faster repeated execution | Compilation time and memory |
| Ahead-of-time compilation | Little run-time compilation | Less adaptation to the actual workload |
HotSpot therefore does not ask, “Can this method be compiled?” Almost every method can be compiled. It asks, “Is compiling this method worthwhile for this run?”
Compilation also changes performance over time. A program may be slower during startup while the interpreter gathers data and compiler threads produce native code. After warmup, hot paths often run faster.
A small demonstration
Compile and run this program with -XX:+PrintCompilation:
public class HotLoop {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
long sum = 0;
for (int i = 0; i < 50_000_000; i++) {
sum += square(i % 1000);
}
System.out.println(sum);
}
}The loop calls square millions of times, so HotSpot has a reason to compile it. The compilation output is evidence that the JVM adapted to the observed workload. The next chapter explains how the C1 and C2 compilers provide different levels of optimization.
Exercise
Why does HotSpot usually interpret bytecode before compiling a method?
Show answer
Interpretation gives fast startup and lets HotSpot collect runtime data, so it can spend compilation time on code that is actually hot.