JIT Optimization in Java: Inlining and Escape Analysis
See how HotSpot handles inlining, escape analysis, scalar replacement, and dead code elimination, and how to inspect JIT behavior.
See how HotSpot handles inlining, escape analysis, scalar replacement, and dead code elimination, and how to inspect JIT behavior.
JIT Optimization Patterns: Inlining, Escape Analysis, and Dead Code Elimination
The Java runtime can interpret bytecode and compile frequently used code while the program runs. A just-in-time (JIT) compiler can use information from that run to optimize code paths, but those decisions depend on the JVM, its version, and the workload.
This guide uses Oracle HotSpot JDK 25 as a reference point. Other HotSpot releases and JVMs can make different choices. Treat the examples as a model for reasoning, not a promise about what every run will compile.
When JIT Knowledge Helps
JIT details are useful when profiling points to a hot method, allocation, or compilation event and you need to understand what the runtime is doing. They can also help explain why a microbenchmark behaves differently from a full application.
For ordinary code, start with a clear implementation and a sound algorithm. Profile before changing code or JVM options. A compiler optimization that helps one workload can have no effect, or make another workload slower.
A Short View of HotSpot Compilation
HotSpot can interpret code and compile methods at different optimization levels. With tiered compilation, the client compiler (C1) can compile code quickly while gathering profile information; the server compiler (C2) can use that information for more heavily optimized code. This is a useful overview, not a fixed route every method follows.
Oracle documents tiered compilation as enabled by default for its HotSpot server VM. Other JVMs and configurations may differ. See the HotSpot performance enhancements guide for the JDK 25 details.
Method Inlining
Inlining replaces a call with the called method’s body in compiled code. This can remove some call overhead, and it lets the compiler optimize across the call boundary.
The compiler weighs several things, including method size, how often a call site runs, and which target types have appeared there. A small method is not guaranteed to inline, and a larger one is not automatically excluded. Decisions can also change as the runtime gathers more profile data.
For example, a getter may be a good inline candidate at a hot call site. That still does not prove that it was inlined or that inlining improved the application. Inspect the relevant runtime’s diagnostics and benchmark the workload before drawing a conclusion.
Inspecting Inlining Decisions
On Oracle HotSpot JDK 25, -XX:+PrintInlining prints inlining decisions and requires -XX:+UnlockDiagnosticVMOptions. -XX:+PrintCompilation prints compilation events. These options produce diagnostic output; they are useful for investigation, not as routine performance settings.
java -XX:+PrintCompilation \
-XX:+UnlockDiagnosticVMOptions \
-XX:+PrintInlining \
-jar application.jar
The JDK 25 java command reference documents these options. Check the documentation for the JVM and release you actually run.
Escape Analysis and Scalar Replacement
Escape analysis examines how a newly created object is used. In HotSpot, this analysis can let the compiler remove some allocations when it can prove the object does not need to exist as a normal heap object in the compiled code.
record Point(int x, int y) {}
static int sum(int x, int y) {
Point point = new Point(x, y);
return point.x() + point.y();
}
If the compiler can see how this point is used and prove its identity is not needed, it may replace the object with its field values. This is called scalar replacement. The source still creates a Point; whether the generated code avoids that allocation depends on the compiler and the surrounding code.
Returning an object reference or storing it somewhere can make the object observable. Inlining and other analysis may still change what the compiler can prove, so the source shape alone is not enough to predict the result.
Escape Analysis Does Not Promise Stack Allocation
Do not assume that an object which does not escape is moved from the heap to the stack. Oracle’s HotSpot documentation says the server compiler can remove scalar-replaceable allocations and associated locks; it also explicitly says that it does not replace heap allocations with stack allocations for objects that do not globally escape.
Escape analysis can also help remove synchronization when the compiler proves a lock is unnecessary. Neither allocation removal nor lock elimination is guaranteed for every object or code path.
Dead Code Elimination
A compiler can remove computation when its result is unused and removing it does not change observable behavior. That last condition matters: an ignored return value does not make a method call safe to delete if the call writes a file, updates shared state, throws an exception, or performs another observable action.
Assertions are controlled by the Java runtime’s assertion settings. The JIT does not simply remove enabled assertions because an application is running in production.
This distinction matters in benchmarks. If a benchmark computes a value and discards it, the compiler may remove work that the benchmark was meant to measure. Use a harness designed for JVM benchmarking, such as JMH, and make the measured result observable to the harness.
Constant Folding and Boolean Expressions
When an expression depends only on constants, a compiler can evaluate it ahead of execution:
int factor = 4 * 5; // can be evaluated as 20
For runtime values, the compiler may simplify an expression when it can prove the transformation preserves Java’s behavior. Do not assume every algebraic rewrite is safe for every type; integer overflow, floating-point rounding, and side effects can matter.
This expression is XNOR: it is true when both operands have the same value.
boolean same = (a && b) || (!a && !b);
For boolean XOR, use:
boolean different = a ^ b;
Deoptimization and Changing Runtime Information
Some compiled code relies on assumptions based on observed types or call patterns. If those assumptions stop holding, HotSpot can deoptimize affected code and later compile it again with updated information. Deoptimization is part of adaptive compilation; its presence alone does not identify a performance problem.
Loading a class does not automatically invalidate every method that has seen a related type. If profiling shows a latency change around class loading or compilation, correlate the events with the workload before attributing the cause.
A Practical Investigation
- Profile the application and identify a repeatable hot path.
- Record the JDK vendor, version, JVM options, hardware, and workload.
- Use JFR or HotSpot compilation diagnostics to inspect what happened.
- Change one thing at a time and compare results with a suitable benchmark.
- Keep a JVM option only when the measurement shows a repeatable benefit.
For a focused HotSpot investigation, -XX:+PrintCompilation can show compilation events. Add -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining when you need to inspect inline decisions. Diagnostic output can affect a run, so do not use that same run as the performance comparison.
The JDK 25 troubleshooting guide covers Java Flight Recorder and other diagnostic tools.
Quick Checklist
- Keep code readable and choose an efficient algorithm.
- Treat inlining and escape analysis as runtime possibilities, not source-level guarantees.
- Do not describe HotSpot escape analysis as general stack allocation.
- Use a JVM-specific diagnostic option only to answer a concrete question.
- Benchmark with a harness, representative inputs, and the JDK used in production.
Interview Questions
Inlining places a method's body at a call site in compiled code. It can reduce call overhead and expose more code to later optimizations. The compiler decides based on its heuristics and profile data; small methods are not guaranteed to inline.
It analyzes how an object reference is used. In HotSpot, the result can enable allocation removal through scalar replacement and can help remove locks when the compiler proves they are unnecessary. These are possible optimizations, not guarantees for each allocation.
Do not rely on that model. Oracle's HotSpot documentation says its server compiler can remove scalar-replaceable heap allocations, but does not replace them with stack allocations. The generated code may avoid an allocation altogether.
Not just because the return value is ignored. The call may have observable effects, such as changing state or throwing an exception. A compiler can remove work only when doing so preserves the program's observable behavior.
It is a runtime transition away from compiled code when an assumption used by that code no longer holds. The JVM can continue execution with less specialized code and may compile again using newer profile information. An event needs workload context before it can be called a performance issue.
For Oracle HotSpot JDK 25, -XX:+PrintInlining prints inline decisions and requires -XX:+UnlockDiagnosticVMOptions. Confirm the option against your JVM's documentation, then use a separate diagnostic run from your benchmark comparison.
Use a JVM benchmarking harness such as JMH, keep the inputs representative, and compare runs on the same JDK and machine. A single hand-timed loop can measure compilation, warmup, or dead-code elimination instead of the operation you intended to compare.
Start with profiling and the JVM's documented defaults. Change a flag only to test a specific hypothesis, and retain it only if repeatable measurements show a benefit for the production workload.
Further Reading
- Async-Profiler — Find CPU and allocation hot spots.
- JVM Flags Explained — Understand JVM options before changing them.
- HotSpot performance enhancements in JDK 25
- The JDK 25 java command reference
- Java Flight Recorder and diagnostic tools
- JMH on OpenJDK
- Advanced Java & JVM Internals Roadmap
Conclusion
Inlining, escape analysis, and dead code elimination can affect compiled Java code, but their results depend on the runtime and workload. In particular, HotSpot escape analysis can enable scalar replacement; it does not promise stack allocation.
When performance matters, measure the application you plan to run. Use diagnostics to form a hypothesis, then compare representative benchmark results on the target JDK before changing code or flags.
Category
Related Posts
Deoptimization Debugging: When JIT Compiled Code Reverts
Learn what causes the JVM to deoptimize JIT-compiled code, how to detect deoptimization events, and how to fix the underlying issues.
JIT Compilation Internals
See how HotSpot tiered compilation works in JDK 25, how to inspect warmup, and when JFR or compilation logs can guide tuning.
Execution Engine: Interpreter, JIT Compiler, and Garbage Collector
Deep dive into the JVM Execution Engine covering bytecode interpretation, JIT compilation, and Garbage Collector architecture and algorithms.