Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Language Features by Version

Java JIT compilation

How the HotSpot JVM turns bytecode into fast machine code: interpreter, C1 and C2 tiers, inlining, escape analysis, deoptimization and warm-up pitfalls.

The Java compiler (javac) does not produce machine code. It produces bytecode, a compact instruction set for an imaginary machine. At run time the JVM executes that bytecode in one of two ways: it interprets it, one instruction at a time, or it compiles it to native machine code with a just-in-time (JIT) compiler. HotSpot, the JVM in OpenJDK and Oracle JDK, does both, and moves code from the first to the second only when it proves worthwhile.

This explains a familiar Java trait: programs start slowly and then speed up. The JVM watches which methods and loops run often (the hot spots) and spends compilation effort only on those, using real profile data such as which branches are taken and which concrete types appear at each call site.

Java
// Nothing to write: the JIT is automatic.
// You influence it with JVM flags such as -XX:TieredStopAtLevel=1

Tiered compilation

HotSpot has two JIT compilers that cooperate in tiers (the default since Java 8):

Tier Executor Purpose
0 Interpreter Runs immediately and collects counters
1 to 3 C1 ("client") compiler Quick compilation, moderate speed; levels 2 and 3 add profiling
4 C2 ("server") compiler Slow compilation, aggressive optimization using the collected profile

A method is compiled when its invocation counter, plus its loop back-edge counter, passes a threshold. A long-running loop inside a method that is called only once can also be compiled while it runs, a technique called on-stack replacement (OSR). Compilation happens on background threads, so your program keeps running in the interpreter meanwhile.

Common optimizations

  • Inlining. Small methods (and, with profiling, hot virtual calls with one dominant receiver type) are pasted into their callers. This removes call overhead and, more importantly, lets later optimizations see across method boundaries. It is the reason that small getters cost nothing.
  • Escape analysis. If an object never leaves the method that creates it, the JIT may skip the heap allocation entirely and keep its fields in registers (scalar replacement).
  • Loop optimizations. Unrolling, hoisting invariant computations out of loops, removing array bounds checks that are provably unnecessary, and vectorizing simple loops with SIMD instructions.
  • Speculation and deoptimization. The JIT may assume, for instance, that a call site only ever sees one class. If the assumption breaks later, the JVM deoptimizes: it throws the compiled code away and falls back to the interpreter, then recompiles with updated knowledge.
  • Intrinsics. Methods such as Math.sqrt, System.arraycopy and many String operations are replaced by hand-tuned native sequences.

Try it

Java The JIT is invisible to results

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Warm-up pitfalls when measuring

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Watching the JIT (read-only)

Terminal
# Read-only: JVM flags are not available in the sandbox
java -XX:+PrintCompilation MyApp                 # one line per compiled method
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining MyApp
java -XX:TieredStopAtLevel=1 MyApp               # C1 only: faster start, lower peak speed
java -Xint MyApp                                  # interpreter only (very slow)
java -XX:CompileThreshold=1000 -XX:-TieredCompilation MyApp  # classic C2-only mode

Beyond just-in-time

  • Class Data Sharing (CDS / AppCDS) stores already parsed class metadata in an archive so JVMs start faster and share memory. A default CDS archive of JDK classes ships with the JDK.
  • Ahead-of-time cache (Project Leyden, JEP 483 and later) stores class loading and linking results from a training run so that startup is faster still. The first part arrived in Java 24; check the release notes of your JDK for what is final.
  • GraalVM Native Image compiles a whole program ahead of time into a standalone executable. It starts in milliseconds but gives up some peak optimizations and dynamic features such as free-form reflection.
  • The Graal compiler is a JIT written in Java that can replace C2 in some JDK distributions.

Usage notes

  • Write clear code first. The JIT is very good at ordinary, idiomatic Java. Hand-inlining methods or avoiding small objects usually helps nothing.
  • Benchmark with JMH, the OpenJDK Java Microbenchmark Harness. It handles warm-up, forks fresh JVMs and prevents dead-code elimination. Hand-rolled timing loops mislead.
  • Megamorphic call sites are slower. If one interface.method() call sees three or more concrete classes, inlining stops and a table lookup is used. This rarely matters outside very hot paths.
  • Huge methods are not compiled. HotSpot refuses to JIT methods larger than 8000 bytes of bytecode (-XX:-DontCompileHugeMethods changes this). Split giant methods.
  • Deoptimization shows up as latency spikes after a long steady state when behavior changes (a new class appears at a hot call site).
  • Containers and short jobs. For command-line tools and serverless functions, CDS, -XX:TieredStopAtLevel=1 or native image often matter more than C2.
  • The code cache has a fixed size; if it fills up, the JIT stops compiling and the program slows down. The warning "CodeCache is full" tells you.