Quick revision
The short answer
On-stack replacement (OSR) lets the JVM switch a method that is already running from interpreted code to compiled code in the middle of a loop. Without it, a method called only once but containing a long loop, such as main or a batch job's processing loop, would stay interpreted for its whole run, because normal compilation only takes effect on the next call. OSR is triggered by back-edge counters in loops, and -XX:+PrintCompilation marks OSR compilations with a % sign.
What to remember
In this answer, we saw that compiled code normally takes effect only on the next call, so a long-running loop in a method called once would stay interpreted. On-stack replacement solves this: back-edge counters detect the hot loop, the JIT compiles a version entered at the loop header, and at the next back edge the interpreter's state is transferred into a compiled frame. PrintCompilation marks these compilations with % and the loop's bytecode index.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
On-stack replacement (OSR) lets the JVM switch a method that is already running from interpreted code to compiled code in the middle of a loop. Without it, a method called only once but containing a long loop, such as main or a batch job's processing loop, would stay interpreted for its whole run, because normal compilation only takes effect on the next call. OSR is triggered by back-edge counters in loops, and -XX:+PrintCompilation marks OSR compilations with a % sign.
Why this question is asked
Most of the JIT's work is driven by method calls: a method is called often, it gets compiled, and the next call uses the compiled code. But some of the hottest code in a program sits in a method that is called once and then loops for a long time. OSR is the mechanism that rescues such code, and it also explains some odd benchmark results. Interviewers ask about it to check that we understand how the JIT decides what and when to compile. In this answer, we will recap how normal compilation takes effect, see the problem a long loop creates, follow how OSR solves it, and read it in PrintCompilation output.
1A Quick Recap: How Compiled Code Normally Takes Over
HotSpot starts every method in the interpreter and counts how often it is invoked. When the count crosses a threshold, the method is queued for compilation, first by C1 and later by C2 for the hottest code. When the compiled version is ready, the method's entry point is switched to it. From then on, every new call runs the compiled code.
Notice the words new call. An invocation that is already running in the interpreter keeps running there, because its state, local variables, operand stack and current position, is held in an interpreter frame, a structure that compiled code does not use. Normally this does not matter, because typical methods are short and are called many times.
2The Problem: A Hot Loop in a Method Called Once
Now consider a method that is called once and runs a loop many millions of times. Its invocation counter is 1, so it never becomes hot by calls. Even if it were compiled, the running invocation would stay in the interpreter until the method returned, which could be minutes later. All the time spent in that loop would run at interpreter speed, often ten times slower or more than compiled code.
The second timing shows what OSR saves. The exact numbers depend on the machine, so treat them as an illustration; the difference is usually large.
3How OSR Works
Besides the invocation counter, the interpreter keeps a back-edge counter. A back edge is the jump at the end of a loop body back to the loop's start, so this counter counts loop iterations. When the back-edge counter of a method crosses its threshold, the JVM requests an OSR compilation for that loop: a special compiled version of the method whose entry point is the loop header, the point where the jump lands, rather than the beginning of the method.
When the compiled version is ready, the next time the running loop takes its back edge, the interpreter does not jump back into interpreted code. Instead it packs up the current state, the values of the local variables such as i and sum and any monitors held, into a buffer, and calls the OSR code with that buffer. The OSR code unpacks the values into its own registers and stack slots and continues the loop from exactly where the interpreter was. The interpreter frame is replaced by a compiled frame on the stack, which is where the name on-stack replacement comes from.
- main() starts in the interpreterinvocation counter: 1
- Loop runs; back-edge counter growsone count per iteration
- Back-edge threshold crossed?Yesrequest OSR compilation for this loopNokeep interpreting
- OSR code readyentry point is the loop header
- At the next back edge: transfer statelocals i, sum and monitors copied
- Loop continues in compiled codethe same invocation, now fast
6 steps in order. Play them, or step through them one at a time.
4Seeing OSR in PrintCompilation
In -XX:+PrintCompilation output, an OSR compilation has a % sign in the attribute column, and the method name is followed by @ and the bytecode index of the loop header where the OSR code is entered.
Reading the example: the loop got a C1 OSR version first, then a C2 OSR version, and the C1 one was retired. So OSR follows the same tiers as normal compilation. The number after @ tells us which loop it was, which helps when a method has several loops.
5Where OSR Matters in Real Code
OSR matters for code with long loops in rarely called methods: batch jobs that process a large file in one method, data migrations, simulation loops, and benchmarks written as one big main method. In request-handling services it matters less, because hot methods are called many times and get normal compilations. OSR code is also not always as good as a normal compilation of the same method, because it has to be entered in the middle and carry the interpreter's state, which is one reason benchmarks written as one loop in main can be misleading.
6Follow-up questions
Does OSR also work in the other direction?
Going from compiled code back to the interpreter in the middle of a method is deoptimisation, a separate mechanism. It happens when compiled code hits an assumption that no longer holds, and it converts compiled frames into interpreter frames so execution can continue. OSR goes from interpreter to compiled code at a loop back edge.
Can a method have both a normal and an OSR compilation?
Yes. If a method with a hot loop is also called often, it can have a normal compiled version, used by new calls, and OSR versions for specific loops, used to take over invocations that are already running in the interpreter. PrintCompilation shows them as separate lines, the OSR ones with % and @ bci.
Should we refactor loops to avoid relying on OSR?
In performance-critical code, moving the loop body into its own method can help, because that method is called many times and gets a normal compilation with full optimisation. For most code, OSR does its job and no change is needed.
7Conclusion
In this answer, we saw that compiled code normally takes effect only on the next call, so a long-running loop in a method called once would stay interpreted. On-stack replacement solves this: back-edge counters detect the hot loop, the JIT compiles a version entered at the loop header, and at the next back edge the interpreter's state is transferred into a compiled frame. PrintCompilation marks these compilations with % and the loop's bytecode index.
Open with
On-stack replacement is how the JVM switches a method that is already running from the interpreter to compiled code, in the middle of a loop. Normally the JIT compiles a method after enough invocations, and the compiled code is used from the next call onwards.
Then, if they want more
That does not help a method that is called once and then loops for a long time, like main in a benchmark or the processing loop of a batch job. Without OSR, that invocation would stay interpreted until it returns, maybe for minutes. So the interpreter also counts back edges, the jumps back to the start of a loop.
When a loop's back-edge counter crosses a threshold, the JIT compiles a special version of the method whose entry point is the loop header.
At the next back edge, the interpreter copies the current local variables and monitors into a buffer and jumps into the compiled code, which continues the loop from the same iteration; the interpreter frame is replaced on the stack by a compiled one.
In PrintCompilation, OSR compilations have a percent sign, and the method is followed by an at sign and the bytecode index of the loop. OSR goes through the tiers like normal compilation, C1 then C2.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is on-stack replacement in the JVM, and why is it needed?": https://scrivio-interview-library.netlify.app/java/jit-compilation/a2a31d75be/