Sub topic 15
Flags for inspecting and tuning the JIT
This chapter assumes you can run java from a terminal. It also assumes you know the tier model from earlier chapters: the interpreter is level 0, C1 produces levels 1 to 3, and C2 produces level 4. Here we turn that model into a working set of flags. Some flags show what the JIT is doing. Others change its behavior. Use the first group freely. Use the second group only when you can measure the effect.
Three kinds of flags
HotSpot flags fall into three groups, and the group tells you how much care a flag needs.
- Product flags such as
-XX:+PrintCompilationwork with no extra option. - Diagnostic flags such as
-XX:+PrintInliningand-XX:+LogCompilationfail at startup unless you add-XX:+UnlockDiagnosticVMOptionsfirst. - Experimental flags need
-XX:+UnlockExperimentalVMOptions. They are unsupported and may change between releases.
To see every flag your JVM knows, with its current value and origin, run this:
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version | grep -i -E "Tier|CICompilerCount|CodeCache|Inline"A := in the output means the value was changed from the default, either by you or by the JVM's ergonomics. Check this before assuming a default.
The test program
The examples below use the hot-loop program from this tutorial.
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);
}
}Inspecting: what was compiled and why
-XX:+PrintCompilation prints one line per compilation event. For a quick look, filter it:
javac HotLoop.java
java -XX:+PrintCompilation HotLoop | grep -E "HotLoop::"Each line has a timestamp in milliseconds and a compile ID. After those come optional flag characters, the tier, the method name, and the size in bytecode bytes. The flag characters you will meet most often are % for an on-stack replacement compile, n for a native wrapper, and s for a synchronized method. A trailing made not entrant means the compiled version will no longer be entered, usually after deoptimization or after a higher tier replaced it.
Inlining decisions need two flags together:
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining HotLoopInlining output shows each call site with a reason such as inline (hot), too big, or callee is too large. It is the quickest way to learn why a small method was not inlined.
For a structured log, use -XX:+LogCompilation. It writes XML to hotspot_pid<pid>.log in the working directory, or to the file named by -XX:LogFile=:
java -XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation -XX:LogFile=jit.log HotLoopThe XML contains compile tasks, inlining trees, and uncommon traps. It is large and meant for tools such as JITWatch rather than for reading by hand.
You can also inspect a JVM that is already running with jcmd:
jcmd <pid> Compiler.queue
jcmd <pid> Compiler.codecache
jcmd <pid> VM.flagsCompiler.queue lists methods waiting for a compiler thread. Compiler.codecache prints code cache usage. VM.flags shows the flag values in effect.
Limiting tiers to compare behavior
These flags change which tiers a method can reach. They are mainly useful for experiments.
| Flag | Effect |
|---|---|
| `-Xint` | Interpreter only. Nothing is compiled. |
| `-XX:TieredStopAtLevel=1` | Stop at C1 without profiling. Fast warmup, lower peak speed. |
| `-XX:-TieredCompilation` | Use the interpreter and C2 only. The interpreter does the profiling. |
| `-Xcomp` | Compile each method on first use. Useful for testing the compilers, not for performance. |
-XX:TieredStopAtLevel=1 suits short-lived tools, because C1 code at level 1 carries no profiling overhead and compile time stays low. It is a poor choice for a long-running server that needs C2's peak speed.
Tuning: thresholds, threads and inlining
Thresholds
With tiered compilation on, the transitions are controlled by flags such as Tier3InvocationThreshold (default 200), Tier3CompileThreshold (2000), Tier4InvocationThreshold (5000), and Tier4CompileThreshold (15000). Tuning each one separately is rarely worthwhile. The single knob worth knowing is -XX:CompileThresholdScaling, which multiplies all of them:
# Compile twice as eagerly
java -XX:CompileThresholdScaling=0.5 HotLoop
# Compile more lazily
java -XX:CompileThresholdScaling=2.0 HotLoopThe older -XX:CompileThreshold (default 10000) applies only when tiered compilation is disabled.
Compiler threads
-XX:CICompilerCount=N sets the total number of compiler threads. With tiered compilation the minimum is 2, one for C1 and one for C2, and the JVM splits the total between them. By default the count depends on the number of CPUs. In a container, check that the JVM sees the CPU limit you expect. A starved compiler queue delays warmup. Too many compiler threads steal CPU from the application.
Compilation is normally in the background, so the application keeps running in the interpreter while a method compiles. -Xbatch (the same as -XX:-BackgroundCompilation) makes the thread wait for the compile to finish. That makes test runs more repeatable. Never use it in production.
Code cache
Compiled code lives in the code cache. Its size is set by -XX:ReservedCodeCacheSize, and the default with tiered compilation is 240 MB. If it fills up, the JVM prints a warning and stops compiling new methods. Performance then falls back toward interpreter speed. Watch for it:
java -XX:+PrintCodeCache HotLoopThe output shows usage for each segment. Raise ReservedCodeCacheSize only if you see it approaching the limit.
Inlining limits
-XX:MaxInlineSize (default 35 bytecode bytes) limits the size of a method that is inlined regardless of how hot it is. -XX:FreqInlineSize (default 325) limits the size of hot methods that can be inlined. Raising these can speed up some code, but it also grows compiled code and compile time. Change them only with a benchmark that proves the gain.
Controlling individual methods
-XX:CompileCommand targets one method without affecting the rest of the program:
# Never inline square
java -XX:CompileCommand=dontinline,HotLoop::square HotLoop
# Never compile square; it stays interpreted
java -XX:CompileCommand=exclude,HotLoop::square HotLoopUse dontinline to isolate an inlining effect in an experiment. Use exclude to work around a suspected compiler bug. In both cases, treat it as a diagnostic tool, not a permanent setting. Quote the argument in a shell if it contains * or $.
Choosing a flag
The diagram summarizes which flag fits which question.
Common mistakes
- Using
-XX:+PrintInliningwithout-XX:+UnlockDiagnosticVMOptions. The JVM refuses to start and names the flag. - Reading
-Xintor-Xcomptimings as evidence about normal performance. Neither matches how the default configuration behaves. - Disabling tiered compilation to "get C2 sooner." Without tiers, the interpreter profiles slowly, so the application usually warms up later.
- Forgetting that
PrintCompilationoutput interleaves with program output. Redirect it to a file or filter it as shown above.
Exercise
A service shows high CPU for its first minute and then settles. In jcmd <pid> Compiler.queue, you often see hundreds of methods waiting. Name two flags you could test, and say what you would measure to decide whether they help.
Show answer
Test -XX:CICompilerCount (more compiler threads, if spare CPUs exist) and -XX:TieredStopAtLevel=1 or -XX:CompileThresholdScaling (less or later compilation work). Measure time to steady-state throughput or latency, CPU use during the first minute, and queue length from Compiler.queue. Compare each against the baseline. Check that peak performance does not drop, especially with TieredStopAtLevel=1, which gives up C2.
Exercise
You run java -XX:+PrintInlining HotLoop and the JVM exits with an error saying the option is diagnostic. What is the fix?
Show answer
Add -XX:+UnlockDiagnosticVMOptions before it: java -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining HotLoop.