Sub topic 5
Long-Running Loops and On-Stack Replacement
Assume you know basic Java methods, loops, bytecode, and command-line use of javac and java. This chapter explains how HotSpot optimizes a loop that is already running.
Why a loop needs special treatment
HotSpot starts by interpreting a method's bytecode. While the method runs, the JVM counts events such as method calls and loop iterations. A loop iteration that jumps back to an earlier instruction is called a back edge.
A method might not be called often, but one invocation can contain a very long loop. Waiting for the method to return before compiling it would waste the opportunity to optimize the loop. HotSpot can instead compile the loop while the method is active. This process is called on-stack replacement, or OSR.
The phrase “on stack” refers to the currently running method frame. After OSR, execution transfers from the interpreted loop to compiled machine code. The method does not restart from the beginning. Local variables and the current loop position are mapped into the compiled version.
A running example
Save this program as HotLoop.java:
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 compilation logging enabled:
javac HotLoop.java
java -XX:+PrintCompilation HotLoopThe output format is implementation-specific, but entries commonly include:
| Output detail | Meaning |
|---|---|
| Method name and signature | The code being compiled |
| `@` with a bytecode index | An OSR compilation at a loop position |
| Compilation level such as `3` or `4` | The tier that produced the compiled code |
| `%` marker in some output | The compilation is an OSR compilation |
The exact threshold depends on the JVM version, hardware, flags, and current workload. Do not treat one observed threshold as a permanent rule.
What happens during OSR
When the back-edge count becomes high enough, HotSpot submits a compilation request. A compiler thread creates machine code for the hot loop. The compiled code needs an entry point that matches the loop's current state, including variables such as i and sum.
When compilation completes, the interpreter reaches a safe transfer point and changes execution to the compiled OSR entry. The loop continues with its current values. The method can later return normally through the compiled code or through a suitable continuation.
OSR code is often less powerful than a complete method compilation because it begins in the middle of a method. After more profiling, HotSpot may compile the whole method at a higher tier. That version can replace the OSR version for future calls.
> callout note > OSR helps long-running loops, but it does not make compilation free. Compilation uses CPU time and memory while the application is running.
Common mistakes
- Assuming every loop is compiled immediately. HotSpot waits for evidence that the loop is worth compiling.
- Confusing OSR with a new method call. OSR changes execution inside the current invocation.
- Comparing a short run with a long run without accounting for compilation time.
- Relying on exact
PrintCompilationoutput across different JDK 11 builds or machines.
Exercise
Why can HotSpot use OSR for a loop that has not yet returned from its method?
Show answer
HotSpot can compile the hot loop from its back-edge count and transfer the current interpreter frame into compiled code, preserving the loop's state instead of waiting for the method to return.