Sub topic 15
Interpreter and JIT Compilation
Assume you can compile and run Java programs and read basic javap -c output. This chapter explains how the JVM turns bytecode into machine instructions while a program runs.
Two ways to execute bytecode
The JVM can execute a method through an interpreter or a just-in-time (JIT) compiler.
The interpreter reads one bytecode instruction at a time and performs its operation. For example, it loads local variables, adds values on the operand stack, and returns a result. Interpretation starts quickly because the JVM does not need to compile the method first.
A JIT compiler translates frequently executed bytecode into native machine code for the current processor. Later calls can run this compiled code directly. The JVM watches execution while the program runs and uses that information to choose compilation targets and optimizations.
Tiered compilation and warm-up
In Java 11 HotSpot, tiered compilation commonly combines several levels:
- The interpreter runs a method immediately.
- A fast compiler may produce modestly optimized native code.
- A more aggressive compiler uses profiling data for stronger optimizations.
A method becomes hot when it is called often or contains a frequently repeated loop. The exact thresholds and compiler behavior depend on the JVM, command-line options, processor, and program workload.
This means a benchmark can be misleading if it measures startup instead of steady-state performance. A program may run slowly during warm-up, then become faster after important methods are compiled.
public class HotLoop {
private static int square(int value) {
return value * value;
}
public static void main(String[] args) {
long sum = 0;
for (int repetition = 0; repetition < 10_000; repetition++) {
for (int value = 0; value < 10_000; value++) {
sum += square(value);
}
}
System.out.println(sum);
}
}Compile and run it with unified JVM logging:
javac HotLoop.java
java -Xlog:compilation=debug HotLoopThe log can show methods being compiled. Do not depend on a particular method or compilation count. Those details are implementation choices.
Optimizations and deoptimization
The JIT compiler uses runtime evidence. If a virtual call repeatedly reaches one implementation, the compiler may inline that method. Inlining replaces a call with the method body, which can expose more optimization opportunities. It may also remove checks, simplify calculations, and eliminate code that cannot affect the result.
These optimizations rely on assumptions. If later execution violates one, the JVM can deoptimize the compiled method. Deoptimization discards or temporarily leaves the compiled path and reconstructs an interpreter-compatible state. Execution then continues safely, and the JVM may compile the method again with different assumptions.
Compilation also consumes CPU time and memory. Very short-lived programs may finish before JIT compilation repays its cost. Long-running services usually benefit more from aggressive optimization.
Choosing execution settings
Use the default tiered compiler for most applications. Investigate options such as -Xint only for experiments or diagnosis; it disables JIT compilation and usually reduces performance. Use a benchmarking framework such as JMH for measurements because it handles warm-up, repeated trials, and compiler effects more carefully than a single System.nanoTime() measurement.
Exercise
Why can the same method run slowly at first and faster later?
Show answer
It may begin in the interpreter, while the JVM collects profiling data and compiles the frequently used method into optimized native code.