Sub topic 4
How HotSpot decides a method is hot
This chapter assumes you can run java from the command line and know the idea of a bytecode interpreter. It also assumes you know that HotSpot compiles some methods to native code while the program runs. Here we cover the mechanism that picks which methods get compiled and when.
Why HotSpot needs a hotness signal
Compiling is not free. A compiler thread uses CPU time and memory, and the resulting native code takes space in the code cache. Most methods in a typical program run only a few times, mostly during startup. Compiling those would cost more than it saves.
HotSpot therefore starts every method in the interpreter and counts how often it runs. Only methods that cross a threshold get queued for compilation. This is a statistical bet: code that has run many times will probably keep running.
The two counters
The interpreter keeps two counters per method.
- The invocation counter is incremented each time the method is entered.
- The back-edge counter is incremented each time a loop jumps back to its start. A back edge is the branch at the end of a loop body. A
forloop that runs 1,000 times adds about 1,000 to the counter of the method that contains it.
Two counters are needed because the invocation count alone misses a method that is called once and loops for minutes. main in the chapter's earlier HotLoop example is that case. The back-edge counter catches it, and the compilation it triggers is the on-stack replacement covered in the next chapter.
The counters live in per-method structures that HotSpot allocates lazily. The interpreter does not check a threshold on every increment. It calls into the runtime only when a counter crosses a notification boundary. By default the invocation counter notifies every 128 increments (Tier0InvokeNotifyFreqLog=7, meaning 2^7). The back-edge counter notifies every 1,024 (Tier0BackedgeNotifyFreqLog=10). At each notification the runtime evaluates the full policy. This keeps interpreter overhead low, but it also means a method is not compiled at exactly the threshold value.
The tiered thresholds
With tiered compilation on, which is the default in JDK 11, two sets of thresholds apply. The Tier3 flags control the move from the interpreter to C1 with full profiling. The Tier4 flags control the move from C1 profiled code to C2.
| Flag | Default | Meaning |
|---|---|---|
| `Tier3InvocationThreshold` | 200 | Invocations that trigger a level 3 compile |
| `Tier3MinInvocationThreshold` | 100 | Minimum invocations when combined with loops |
| `Tier3CompileThreshold` | 2000 | Invocations plus back edges, if above the minimum |
| `Tier3BackEdgeThreshold` | 60000 | Back edges that trigger an OSR compile at level 3 |
| `Tier4InvocationThreshold` | 5000 | Invocations that trigger a level 4 (C2) compile |
| `Tier4MinInvocationThreshold` | 600 | Minimum invocations when combined with loops |
| `Tier4CompileThreshold` | 15000 | Invocations plus back edges, if above the minimum |
| `Tier4BackEdgeThreshold` | 40000 | Back edges that trigger an OSR compile at level 4 |
Defaults can differ between builds, so check your own JVM. The command java -XX:+PrintFlagsFinal -version | grep Tier prints the values it actually uses.
The decision rule
Let i be the invocation count, b the back-edge count, and s a scale factor (described below). A method is queued for a Tier3 compile when either condition holds:
i > Tier3InvocationThreshold * si > Tier3MinInvocationThreshold * sandi + b > Tier3CompileThreshold * s
The second condition lets a method that is called a moderate number of times, and also loops, qualify sooner than the first rule alone would allow. The Tier4 check has the same shape with the Tier4 flags. A loop-only check applies separately: when b exceeds the BackEdge threshold times s, HotSpot requests an OSR compilation.
Load feedback: the scale factor
The scale s is not fixed. HotSpot adjusts it using the length of the compile queue:
s = queue_length / (TierNLoadFeedback * compiler_thread_count) + 1
The defaults are Tier3LoadFeedback=5 and Tier4LoadFeedback=3. When many methods are waiting, s grows and thresholds rise. This stops a burst of hot methods, such as at startup, from flooding the queue and delaying the compiles that matter most. When the queue drains, thresholds return to their base values.
Scaling every threshold at once
You rarely need to edit each flag. -XX:CompileThresholdScaling=<factor> multiplies all the thresholds above. A value of 0.1 makes compilation about ten times more eager. A value of 10 makes it about ten times more reluctant. This is useful for experiments, such as forcing compilation in a short test so you can look at the output.
Seeing the thresholds at work
The program below calls work in batches of increasing size and prints a marker between batches. Compilation lines from -XX:+PrintCompilation appear interleaved with those markers.
public class Counters {
static int work(int x) {
if ((x & 1) == 0) {
return x * 31 + 7;
}
return x / 3 - 5;
}
public static void main(String[] args) throws Exception {
int[] batches = {50, 150, 300, 5_000, 100_000};
int acc = 0;
for (int b : batches) {
for (int i = 0; i < b; i++) {
acc += work(i);
}
System.out.println("after batch of " + b);
Thread.sleep(100);
}
System.out.println(acc);
}
}The Thread.sleep gives the background compiler threads time to finish and print before the next batch begins.
javac Counters.java
java -XX:+PrintCompilation Counters | grep -E "Counters::work|after batch"
# Make thresholds ten times lower
java -XX:+PrintCompilation -XX:CompileThresholdScaling=0.1 Counters | grep -E "Counters::work|after batch"With default settings you would typically see no compile line for work after the batches of 50 and 150. A level 3 line usually appears during or just after the batch of 300, because the cumulative count passes 200. A level 4 line usually appears during the later batches, after about 5,000 invocations. With scaling at 0.1, all of these move earlier. Exact numbers vary because of notification intervals, queue length, and timing.
How the decision flows
Common mistakes
- Expecting an exact trigger. Notification intervals and queue-based scaling mean "after 200 calls" is only approximate.
- Forgetting loops. A method with a long loop can become hot on back edges alone, with a very low invocation count.
- Testing with tiny inputs. If your test never reaches the thresholds, you are measuring interpreter speed, not JIT speed.
- Changing one threshold in isolation. Lowering
Tier4InvocationThresholdhas no effect if the method never reaches level 3 first. UseCompileThresholdScalingunless you know which transition you are tuning. - Assuming the flag defaults. They are build-specific. Print them with
-XX:+PrintFlagsFinal.
Exercise
A method is called 150 times and contains a loop that runs 20 times per call. Using the default Tier3 thresholds and a scale of 1, does it qualify for a level 3 compile? Show the arithmetic.
Show answer
Yes. After 150 calls, i = 150 and b is about 150 * 20 = 3,000. The first rule fails because 150 is not greater than 200. The second rule holds: i = 150 is greater than Tier3MinInvocationThreshold (100), and i + b = 3,150 is greater than Tier3CompileThreshold (2,000). The method qualifies, although the actual compile is requested only at the next notification boundary.
Exercise
Why does HotSpot raise the effective thresholds when the compile queue is long, and which flags control how strongly it does so?
Show answer
A long queue means compiler threads are already behind. Adding more requests would delay the compiles of the hottest methods. HotSpot scales thresholds up by queuelength / (TierNLoadFeedback * compilerthread_count) + 1, so Tier3LoadFeedback (default 5) and Tier4LoadFeedback (default 3) control the strength. A smaller feedback value raises thresholds faster for the same queue length.