Sub topic 15
Flags for Inspecting and Tuning the JIT
Assume that you know basic Java, can compile a class with javac, and can run programs with java. You should also know that HotSpot compiles frequently executed bytecode into native machine code while a program runs.
JIT flags are diagnostic tools first. Use them to observe behavior before changing it. Many flags are implementation details, so check them on the exact JDK and version you use.
Inspecting compilation
Create a small program with a frequently called method:
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 with PrintCompilation:
javac HotLoop.java
java -XX:+PrintCompilation HotLoopHotSpot prints compilation events as they occur. A line can contain a compilation ID, a method name, a bytecode size, and a compilation level. For example, a line ending in % often represents on-stack replacement, where HotSpot replaces interpreted code inside a running loop.
The output order is not a complete timeline of the program. Compiler threads work in the background, so compilation messages can appear while the main thread continues to run.
Use PrintFlagsFinal to inspect the available values on your JVM:
java -XX:+PrintFlagsFinal -versionThis output helps you distinguish a default value from a value supplied by the command line. It also avoids assuming that a flag has the same default on every Java release.
Comparing execution modes
The following commands provide useful experiments:
# Show compilation events with normal tiered compilation
java -XX:+PrintCompilation HotLoop
# Interpret bytecode without JIT compilation
java -Xint HotLoop
# Stop tiered compilation at C1 level 1
java -XX:TieredStopAtLevel=1 HotLoop
# Disable tiered compilation and use the traditional C2 path
java -XX:-TieredCompilation HotLoopThese modes are useful for diagnosis, not as automatic performance improvements. -Xint usually makes a long-running program much slower. Limiting compilation can reduce optimization quality, even if startup becomes simpler.
| Flag | Main use | Typical caution |
|---|---|---|
| `-XX:+PrintCompilation` | Show compilation events | Output is implementation-specific |
| `-XX:+PrintInlining` | Show inlining decisions | Requires diagnostic options on some JDK builds |
| `-XX:TieredStopAtLevel=1` | Limit tiered compilation | Prevents higher-level optimization |
| `-XX:-TieredCompilation` | Disable tiered compilation | Changes the normal JDK 11 execution path |
| `-Xint` | Run in the interpreter | Usually very slow |
Inspecting inlining
Inlining replaces a method call with the method body. It can remove call overhead and expose more code to later optimizations. You can request diagnostic inlining output:
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining HotLoopInlining is affected by method size, call-site frequency, type information, and compiler budgets. A method that is not inlined is not necessarily a performance problem.
A practical tuning rule
First measure the application with its normal flags. Then compare one controlled change against that baseline. Run enough iterations for compilation and warmup, use the same input, and record the JDK version and hardware. Treat a flag as a hypothesis test, not as a permanent fix.
Exercise
Which command should you use to see whether HotSpot is compiling methods while HotLoop runs, and why should you avoid judging performance from the first few milliseconds?
Show answer
Run java -XX:+PrintCompilation HotLoop. The first part of execution includes interpretation, profiling, and compilation, so it may not represent the steady-state performance of compiled code.