Sub topic 8
Watching the JIT with PrintCompilation
Assume you know basic Java, bytecode, and command-line use of javac and java. This chapter shows how to inspect HotSpot's just-in-time (JIT) compilation with -XX:+PrintCompilation.
Run a program that becomes hot
The JIT compiles code that runs often. This program calls square inside a long loop:
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);
}
}Compile and run it as follows:
javac HotLoop.java
java -XX:+PrintCompilation HotLoopThe output includes many JDK methods as well as methods from HotLoop. It varies between runs because compilation happens concurrently and depends on the machine.
A simplified part of the output might look like this:
1 b 3 java.lang.String::hashCode (55 bytes)
105 3 3 HotLoop::square (5 bytes)
121 4 4 HotLoop::square (5 bytes)
130 % 4 HotLoop::main @ 8 (35 bytes)Read the compilation columns
The important fields are:
| Output part | Meaning |
|---|---|
| `105` | Compilation identifier |
| `%` | An on-stack replacement compilation |
| `3` or `4` | The tier selected for the compiled code |
| `HotLoop::square` | The compiled method |
| `(5 bytes)` | Bytecode size |
| `@ 8` | Loop bytecode position used for OSR |
The first number is an identifier for that compilation task. It is not a performance score.
With tiered compilation, level 3 usually means C1 compiled code with profiling. Level 4 usually means C2 optimized code. A method can appear more than once as HotSpot replaces an earlier version with a more optimized version.
The % marker matters for long loops. HotSpot can compile a loop while the method is already running, then transfer the active execution into the compiled loop. This process is called on-stack replacement, or OSR.
Other markers can appear. For example, s indicates a synchronized method, and ! indicates exception-handling information. A later line such as made not entrant means compiled code should no longer receive new calls. The JVM may have found a better version or invalidated an assumption.
Inspect different execution modes
These commands help compare the default behavior with restricted modes:
java -XX:+PrintCompilation HotLoop
java -Xint HotLoop
java -XX:+PrintCompilation -XX:TieredStopAtLevel=1 HotLoop
java -XX:+PrintCompilation -XX:-TieredCompilation HotLoop-Xint disables JIT compilation and uses the interpreter only. TieredStopAtLevel=1 prevents normal higher-tier compilation. -XX:-TieredCompilation selects the older non-tiered strategy, where C2 is used after interpreter profiling.
Do not treat these modes as production tuning recommendations. They are useful for experiments and diagnosis. Compilation output alone also does not prove that one run is faster. Use a benchmark with warmup and repeated measurements.
Exercise
In a PrintCompilation line, what does the % marker usually indicate?
Show answer
It usually indicates on-stack replacement: HotSpot compiled a loop while its containing method was already running.