Sub topic 12
Deoptimization: undoing a bad assumption
This chapter assumes you have read the earlier chapters on tiers, profiling, inlining and speculative optimization. You should know that C2 compiles code using what the profile says about the running program, and that it leaves uncommon traps where the profile says a path never happens.
Why deoptimization exists
C2 produces fast code by betting on the profile. If a call site has only seen Square, C2 compiles it as "check that the receiver is Square, then run the inlined body". If a branch was never taken, C2 may leave it out and put a trap there.
Programs change behavior, so some bets will lose. Deoptimization is how HotSpot recovers. It moves execution out of compiled code that rests on a broken assumption and back to a state that is always correct, which is the interpreter. It also discards or invalidates the bad compiled code so no new calls enter it.
Without deoptimization, C2 could only make optimizations that are valid for every possible future execution. That would rule out most of what makes it fast.
Two ways deoptimization is triggered
Uncommon traps in compiled code
A speculative check inside the compiled code fails. The code calls into the VM through an uncommon trap. Common trap reasons, as they appear in logs, include:
| Reason | Meaning |
|---|---|
| `class_check` | The receiver was not the single type the profile predicted. |
| `bimorphic` | A call site that was bimorphic (two types) saw a third type. |
| `null_check` | A reference the profile said was never null was null. |
| `range_check` | An array index failed an assumption made by the compiler. |
| `unstable_if` | A branch that the profile said was never taken was taken. |
| `unloaded` | Code reached a class that was not yet loaded when it was compiled. |
Invalidated dependencies
C2 also records what it assumed about the class hierarchy. For example, if only one loaded class implements Shape, a call to area() can be devirtualized without any runtime check. HotSpot records that this compiled method depends on this fact (class hierarchy analysis, or CHA). When a new class is loaded and the fact stops being true, HotSpot finds every compiled method with that dependency and invalidates it.
What happens during a trap
- Compiled code reaches the trap and calls into the VM.
- The VM reads debug information that the compiler stored for that exact point. It describes where each Java local, stack slot and lock lives in registers or stack memory.
- The VM rebuilds interpreter frames from that data. A compiled frame that had inlined three methods becomes three interpreter frames.
- Execution continues in the interpreter at the right bytecode, so the program sees no change in behavior.
- The VM decides what to do with the compiled method, based on the trap's action. Usually it makes the method not entrant, so new calls stop entering it.
- The method's counters and profile continue to grow. When it is hot again, it is recompiled, now with the new type or branch in the profile.
Seeing it in PrintCompilation
Run the DeoptDemo program from the chapter's code examples:
javac DeoptDemo.java
java -XX:+PrintCompilation DeoptDemoYou will usually see output of this shape. The timestamps, ids and sizes differ from run to run, so treat this as an illustration and not as exact output.
85 31 4 DeoptDemo::run (6 bytes)
102 31 4 DeoptDemo::run (6 bytes) made not entrant
103 45 4 DeoptDemo::run (6 bytes)Read it as follows:
- The columns are the timestamp in milliseconds, the compile id, the tier, and the method.
- The first line is
runcompiled at tier 4 while onlySquarehad been seen. made not entranton the same id means that compiled code can no longer be entered by new calls. This is the visible sign of a deoptimization or of a replaced compilation.- The third line is a new compile id for the same method. This is the recompilation, built from the profile that now includes
Circle.
A method at a lower tier can also show made not entrant when a higher-tier version replaces it. That is normal tier promotion, not a failed bet. To tell the two apart, check whether the new version appears at a higher tier (promotion) or at the same or a lower tier after the trap (deoptimization).
In JDK 11 you may also see made zombie later. It means the code is no longer on any stack and its memory can be reclaimed.
Seeing the reason
PrintCompilation does not say why a method was deoptimized. For that, use the diagnostic compilation log:
java -XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation \
-XX:LogFile=deopt.log DeoptDemo
grep uncommon_trap deopt.logThe log is XML. Each uncommon_trap element names a reason such as class_check and an action such as reinterpret or make_not_entrant, along with the method and bytecode index. The log can be large, so filter it as shown.
For DeoptDemo, the likely cause is the class_check on the receiver of s.area(). The class loading of Circle may also invalidate a CHA dependency, depending on what C2 had assumed. Both paths end in the same visible result.
Actions and limits
Each trap has an action that says what to do with the compiled code:
none: keep the compiled code. Used for traps that do not change the assumptions.maybe_recompile: keep the code for now, but let the trap counter decide whether to recompile.reinterpret: invalidate the code and reset the method's profile to get a fresh view.make_not_entrant: invalidate the code, and recompile when hot again.make_not_compilable: stop trying to compile this method at this tier.
A site that keeps failing would otherwise cycle forever between compile and deopt. HotSpot limits this by counting traps per bytecode and per method (the PerBytecodeTrapLimit and PerMethodTrapLimit flags). When a limit is reached, the compiler stops speculating on that site and emits a general, slower but safe path. This is why a method that deoptimizes a few times usually settles down. It is also why the third compile of run is typically more general than the first.
Costs and failure modes
- Slow transition. The trap itself, the frame rebuilding and the interpretation until recompilation are all slower than normal execution. For a hot method, this can be a short latency spike.
- Late surprises. A rare branch can be taken hours into a run, for example on an error path. The method is deoptimized then, long after benchmarks would have finished.
- Recompile storms. When a program changes phase (loading a plugin, switching workloads), many methods can be invalidated together, and compiler threads get busy at once.
- Megamorphic sites. If a call site really sees many types, C2 gives up on inlining it. This is a stable slow-but-correct path and not a deoptimization loop.
Practical checklist
- Run with
-XX:+PrintCompilationand search formade not entranton the method you care about. - Compare tiers on the neighboring lines to tell promotion from deoptimization.
- Use
-XX:+UnlockDiagnosticVMOptions -XX:+LogCompilationto get the trap reason and bytecode index. - Check whether the event happens during warmup or in steady state.
- If it happens in steady state, look for a changing input shape at that site.
Exercise
In PrintCompilation output you see the same method at tier 3 marked "made not entrant", followed immediately by a new compile id for that method at tier 4. Was this a deoptimization caused by a failed assumption? Explain how you would confirm it.
Show answer
Probably not. A tier 3 version being replaced by a tier 4 version is normal promotion, so the old code is made not entrant because a better version exists. A deoptimization would more likely show a tier 4 (C2) version made not entrant, followed by a recompilation at the same or a lower tier. To confirm, run with -XX:+UnlockDiagnosticVMOptions -XX:+LogCompilation and search the log for uncommon_trap elements that name the method. If there is none, no assumption failed.
Exercise
Name the two kinds of events that trigger deoptimization of C2-compiled code, and give an example of each using the DeoptDemo program.
Show answer
First, a failed runtime check in the compiled code (an uncommon trap), such as the class_check that fails when run(Shape) receives a Circle after compiling for Square. Second, an invalidated class hierarchy dependency, such as loading Circle after C2 had assumed that Square was the only loaded implementation of Shape.