Sub topic 8
Watching the JIT with PrintCompilation
This chapter assumes you know the tier model from the previous chapters: levels 0 to 4, C1 and C2, invocation and back-edge counters, and on-stack replacement (OSR). You also need to be comfortable running java from a shell. Here you will learn to read the output of -XX:+PrintCompilation, the simplest way to see the JIT at work.
What the flag prints
-XX:+PrintCompilation is a standard HotSpot product flag. It needs no unlock option. Each time the JVM finishes a compilation task, or retires compiled code, it prints one line to standard output. Because this goes to the same stream as System.out, JIT lines and program output are interleaved.
Use the HotLoop program from the spine:
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, keeping only the lines that mention the class:
javac HotLoop.java
java -XX:+PrintCompilation HotLoop | grep HotLoopThe following output is illustrative. Timestamps, ids, and the exact tier sequence differ between machines and JDK 11 builds.
61 45 3 HotLoop::square (4 bytes)
62 46 % 3 HotLoop::main @ 8 (35 bytes)
63 47 4 HotLoop::square (4 bytes)
65 48 % 4 HotLoop::main @ 8 (35 bytes)
65 45 3 HotLoop::square (4 bytes) made not entrant
66 46 % 3 HotLoop::main @ 8 (35 bytes) made not entrantReading one line
Take the line 62 46 % 3 HotLoop::main @ 8 (35 bytes). In JDK 11 the columns, from left to right, are:
| Column | Example | Meaning |
|---|---|---|
| Timestamp | `62` | Milliseconds since JVM start when the event was printed |
| Compile id | `46` | Sequence number of the compilation task |
| Attribute flags | `%` | Up to five characters, described below |
| Tier | `3` | Compilation level, 1 to 4 |
| Method | `HotLoop::main` | Class and method name |
| OSR bytecode index | `@ 8` | Present only for OSR compiles. It is the loop's bytecode index. |
| Size | `(35 bytes)` | Size of the method's bytecode, not of the machine code |
| Comment | `made not entrant` | Optional note about what happened to the code |
The attribute flags are:
%marks an OSR compilation, started by a hot loop's back-edge counter.smarks a synchronized method.!marks a method with an exception handler.bmarks a compile that blocked the requesting thread (a foreground compile).nmarks a native wrapper.
Level 0 never appears, because the interpreter does not produce compilation tasks. Interpreted execution is the absence of a line.
Reading the story in the output
The sample above shows the usual progression:
squareis compiled at tier 3 (C1 with full profiling). It was invoked often enough to cross the tier 3 threshold.maingets an OSR compile at tier 3, marked%with@ 8. The loop ran long, butmainwas only invoked once, so only the back-edge counter could trigger a compile.- Both are recompiled at tier 4 (C2) once the profile data is mature.
- The tier 3 versions are marked
made not entrant. The tier 4 code has replaced them, so no new call may enter the old code.
Compile ids belong to the task, not the method. The same id can appear twice, once when the code is created and again when it is retired. That is why id 45 appears at both the start and the end.
A trivial method, such as a simple getter, can skip the profiling tiers. It is compiled at tier 1 (C1 without profiling), because C2 cannot make it faster. A tier 1 method is usually never recompiled.
Retired code: not entrant and zombie
Two comments describe the end of a code version's life:
made not entrantmeans no new invocation may enter this code. Threads already running in it may finish. This happens when a better tier replaces it or when a speculative assumption fails.made zombiemeans no thread is executing the code anymore, so the code cache can reclaim the memory. You will see this comment in JDK 11 output.
Do not use the empty block above. The diagram follows.
Seeing deoptimization
DeoptDemo from the spine is designed to trigger a retired compile because of a failed assumption:
public class DeoptDemo {
interface Shape { double area(); }
static final class Square implements Shape {
public double area() { return 4.0; }
}
static final class Circle implements Shape {
public double area() { return 3.0; }
}
static double run(Shape s) {
return s.area();
}
public static void main(String[] args) {
double total = 0;
Shape square = new Square();
for (int i = 0; i < 5_000_000; i++) {
total += run(square);
}
Shape circle = new Circle();
for (int i = 0; i < 5_000_000; i++) {
total += run(circle);
}
System.out.println(total);
}
}javac DeoptDemo.java
java -XX:+PrintCompilation DeoptDemo | grep DeoptDemoDuring the first lo Square. C2 can then compile run assuming s.area() always resolves to Square.area(), and inline it. When Circle arrives, the guard fails and execution falls back to the interpreter. Typical output then includes a line like this:
118 62 4 DeoptDemo::run (6 bytes) made not entrantLater, run is compiled again, this time with a profile that has seen two receiver types. The exact lines depend on timing. If you see no made not entrant line for run, the method may not have reached tier 4 before Circle appeared. Increase the first loop's count and run again.
Practical filtering
A real application prints thousands of lines, and most come from JDK internals. These habits keep the output readable:
- Filter by class:
java -XX:+PrintCompilation App | grep 'com.example'. - Redirect to a file and search later, so JIT lines do not drown program output.
- Look for runs of
made not entranton the same method. Repeated retirement means the method keeps being compiled and invalidated. - Compare tier numbers to see how long a hot method stayed at tier 3. Code at tier 3 runs noticeably slower than tier 4 because of profiling overhead.
Common mistakes when reading the output
- Treating the timestamp as compile duration. It is the time of printing, not the time the compile took.
- Reading the size as machine code size. The number in parentheses is bytecode bytes. Compiled code is usually larger.
- Expecting identical output between runs. Compilation happens on background threads, and thread timing, machine load, and the number of CPUs change the order and the timing.
- Assuming every
made not entrantis a problem. Most come from normal tier promotion, as in the first sample. Only repeated retirement of a method at tier 4 deserves investigation. - Comparing runs with different flags.
-XX:TieredStopAtLevel=1and-Xintchange what is printed. Compare like with like.
Exercise
In the line 88 31 % 4 Report::build @ 21 (120 bytes), what do the %, the 4, and the @ 21 tell you about this compilation?
Show answer
The % shows it is an on-stack replacement compile. The 4 shows it was compiled by C2 at tier 4. The @ 21 is the bytecode index of the loop where execution can switch into the compiled code. Together they mean build contains a long-running loop that was hot enough to be replaced while the method was still running. The 120 bytes is the size of the method's bytecode.
Exercise
You run java -XX:+PrintCompilation App and see Parser::next compiled at tier 4, then made not entrant, then compiled at tier 4 again, several times. What does this suggest, and which flag would you add to look further?
Show answer
Repeated retirement at tier 4 suggests that C2 keeps making a speculative assumption that later fails, so the code is deoptimized and recompiled. Adding -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining helps show what was inlined. -XX:+LogCompilation records the reasons for uncommon traps.