Java / JVM Architecture (Java21) Interview questions
What are OSR (On-Stack Replacement) compilations, and when do they happen?
On-Stack Replacement (OSR) lets the JVM swap a method that is currently executing in the interpreter for a JIT-compiled version, mid-execution, without waiting for that method call to return first.
It exists specifically for long-running loops: a method's normal invocation counter only increments once per call, but a loop's back-edge counter increments on every iteration, so a method called just once but looping millions of times can become "hot" and eligible for compilation long before it would ever return.
When the back-edge counter crosses its threshold, HotSpot compiles a special OSR-entry version of the method starting at the current loop point, transfers the interpreter's live local variables into the new compiled frame, and continues execution in native code from there.
Without OSR, a program dominated by one very long-running loop, common in numeric or batch-processing code, would never benefit from JIT compilation at all, since a normal invocation-count trigger would never fire.
More Related questions...