Just-In-Time (JIT) Compilation in the JVM

Just-In-Time Compilation (JIT) is a compilation technique that compiles code right as it is about to run for the first time, which is where the name “just-in-time” comes from. Strictly speaking, JIT is a specific form of dynamic compilation. Over time, the term broadened and is now commonly used to refer to dynamic compilation in general, though a distinction still exists between strict JIT and dynamic compilation at large. Dynamic compilation compiles code at runtime, whereas static compilation (also called Ahead-Of-Time Compilation, or AOT) compiles code before the program runs. Adaptive dynamic compilation is another form of dynamic compilation: it first lets the program run in one mode to collect profiling data, then compiles it later for deeper optimizations.

Overview

In commercial JVMs like HotSpot, Java programs initially run through an interpreter. When the JVM notices that a method or code block is called frequently, it marks that code as “hot code.” To speed it up, the JVM compiles the hot code at runtime into native machine code optimized for the local platform. This is the job of the Just-In-Time (JIT) compiler.

A JIT compiler isn’t required in a JVM. The Java Virtual Machine Specification neither mandates one nor dictates how it should be built. Still, the performance and optimization quality of a JIT compiler are key measures of any commercial JVM and a big part of its engineering strength. Unless noted otherwise, “compiler” and “JIT compiler” in this post refer to HotSpot’s JIT compilers, and “JVM” refers specifically to HotSpot.

Mixed-mode architecture: interpreter and compiler together

Not every JVM pairs an interpreter with a compiler, but most mainstream commercial VMs, including HotSpot, run both side by side. Each brings its own strengths:

  • Interpreter: When a program needs to start up and run right away, the interpreter can get to work immediately without waiting for compilation.
  • Compiler: As the program keeps running, the compiler gradually kicks in, translating more and more code into native machine code for higher execution speed.

In memory-constrained environments like embedded systems, sticking to interpreted execution saves memory; elsewhere, compiled execution gives better performance. Also, if compiled code hits a rare edge case, the JVM can fall back to the interpreter to recover.

Time and space overhead of compilation

Time overhead

  • Interpreted execution: input code → interpreter executes → result
  • JIT compilation and execution: input code → compiler compiles → compiled code → execution → result

JIT-compiled code generally runs faster than interpreted code, but that doesn’t mean compilation itself is faster than interpretation. For code that only runs once, interpreting it is usually faster because compilation adds upfront overhead. JIT only pays off when code runs frequently.

Space overhead

Compiled native code is much larger than bytecode; a 10x expansion ratio is common. That’s another reason to compile only hot code. Compiling everything would significantly increase the code footprint.

Different compilers

HotSpot ships with two built-in JIT compilers:

  • Client Compiler (C1): Aimed at client applications, prioritizing fast compilation speed.
  • Server Compiler (C2): Aimed at server applications, prioritizing peak compiled code quality.

The JVM automatically picks an execution mode based on its version and the host hardware, or you can force client or server mode using the -client or -server flags. Offering multiple JIT compilers serves the same purpose as offering multiple garbage collectors: fitting different workloads.

Hot code

Definition

Hot code refers to sections of code invoked frequently at runtime, including both methods and loop bodies. Because they run so often, they have a big impact on overall performance.

Detecting hot code

JIT compilers detect hot code in two main ways:

  • Counter-based hot spot detection: Attach counters to each method and loop body to track how many times they run. Once a counter crosses a threshold, the code is flagged as hot.
  • Sample-based hot spot detection: Periodically inspect the top of each thread’s call stack. Methods that repeatedly show up at the top are considered hot.

HotSpot primarily uses counter-based detection. Each method maintains two counters: an invocation counter and a back-edge counter. When the counts exceed their threshold, JIT compilation is triggered.

Invocation counter

The invocation counter tracks how many times a method has been called. When a method is invoked, the JVM first checks whether a compiled version already exists and runs that if available. If not, it increments the invocation counter and checks whether it has crossed the threshold. Once it does, a compilation request is submitted to the JIT compiler. When compilation finishes, the method’s entry point is automatically patched to jump to the newly compiled version.

Back-edge counter

The back-edge counter tracks how many times loop bodies inside a method execute. In bytecode, an instruction that jumps control flow backward is called a “back edge.” The back-edge counter is added to the invocation counter, and when the combined total crosses the threshold, JIT compilation is triggered as well.

Summary

JIT compilation speeds up programs substantially by compiling hot code at runtime. HotSpot pairs an interpreter with JIT compilers and uses counter-based hot spot detection to find and optimize hot paths, which keeps performance strong across different workloads.