Sub topic 4
How HotSpot Decides a Method Is Hot
This chapter assumes that you know basic Java syntax, can compile and run a Java class, and understand that the JVM executes Java bytecode. The focus here is how HotSpot notices code that runs often enough to deserve compilation.
The two kinds of execution counts
HotSpot starts by interpreting bytecode. While the interpreter runs a method, it records how often important events occur:
- An invocation count records how many times a method starts.
- A back-edge count records how often a loop jumps back to an earlier bytecode instruction.
A method can become hot because many calls enter it. A method can also become hot during one call if it contains a long-running loop.
Consider this method:
static long sumSquares(int limit) {
long sum = 0;
for (int i = 0; i < limit; i++) {
sum += (long) i * i;
}
return sum;
}If the program calls sumSquares once with a very large limit, its invocation count is only one. Its loop back-edge count, however, becomes very large. HotSpot can request on-stack replacement, or OSR, to replace the currently running interpreted loop with compiled machine code. The method does not need to return and start again before benefiting from compilation.
How a compilation request is triggered
HotSpot compares execution counts with thresholds. A threshold is a value at which the JVM considers requesting compilation. The exact behavior depends on the JVM version, flags, tiered compilation settings, and runtime feedback.
For a frequently called method, the process is roughly:
- The interpreter enters the method and updates its counters.
- The counters approach a compilation threshold.
- HotSpot places a compilation request in a compiler queue.
- A compiler thread produces native code in the background.
- Later calls use the compiled version.
For a long loop, the back-edge counter can trigger an OSR compilation. The running thread transfers from the interpreter into compiled code at a safe point in the loop.
A counter is evidence of execution frequency, not a promise that compilation will happen immediately. The compiler queue can be busy, and tiered compilation can choose an intermediate compilation level first.
Counters and tiered compilation
With tiered compilation enabled, HotSpot uses several levels. Early compilation may use the fast C1 compiler and limited profiling. Later compilation may use C2 with richer profile data.
The thresholds are therefore connected to the current tier. A method may be compiled before it reaches the threshold you expected from a non-tiered JVM. Some thresholds are also adjusted as the JVM gathers information about the application.
Counters can be interpreted as follows:
| Counter | What it measures | Typical consequence |
|---|---|---|
| Invocation count | Method entries | Normal method compilation |
| Back-edge count | Loop iterations | OSR compilation for an active loop |
| Profile data | Types, branches, and call behavior | Better later optimization decisions |
HotSpot also manages counter data over time. Old information can lose influence, which helps the JVM adapt when an application's behavior changes. A method that was hot during startup may no longer deserve aggressive optimization after the workload moves elsewhere.
A small experiment
Compile and run this class:
public class HotMethod {
static int square(int value) {
return value * value;
}
public static void main(String[] args) {
long total = 0;
for (int i = 0; i < 20_000_000; i++) {
total += square(i % 1000);
}
System.out.println(total);
}
}Run it with:
javac HotMethod.java
java -XX:+PrintCompilation HotMethodThe output is a stream of compilation events. The exact order varies between runs and machines. Look for square and main. The loop makes main hot, while repeated calls make square a compilation candidate.
Exercise
Why can a method with one invocation still be compiled?
Show answer
Its loop back-edge count can become large enough to trigger OSR compilation while that invocation is still running.