2.2 Bytecode & the JVM Execution Engine
← Back to Chapter 2: Java Overview & Memory Model
Understanding how the JVM processes compiled bytecode into raw CPU micro-instructions is critical for performance tuning and memory diagnosis.
1. Class Loading Mechanism (The 3 Phases)
Before a class can execute, the JVM ClassLoader Subsystem processes it through three distinct phases:
┌─────────────────┐ ┌─────────────────────────┐ ┌────────────────────┐
│ 1. LOADING │ ───► │ 2. LINKING │ ───► │ 3. INITIALIZATION │
│ Bootstrap CL │ │ • Verification (safety) │ │ Executes static │
│ Platform CL │ │ • Preparation (defaults)│ │ initializers & │
│ Application CL │ │ • Resolution (sym->dir) │ │ static blocks │
└─────────────────┘ └─────────────────────────┘ └────────────────────┘
- Loading: Reads
.classbinary stream and creates ajava.lang.Classobject in Metaspace. - Linking:
- Verification: Confirms bytecode follows JVM specifications and cannot corrupt registers/pointers.
- Preparation: Allocates static memory and initializes static fields to standard defaults (
0,null). - Resolution: Replaces symbolic references in the Constant Pool with direct memory addresses.
- Initialization: Invokes
<clinit>()to execute explicit static variable initializers andstatic { ... }blocks.
2. The Execution Engine: Interpreter vs JIT Compiler
The execution engine translates bytecode into native CPU machine instructions:
┌───────────────┐
│ Bytecode │
└───────┬───────┘
│
┌────────────────┴────────────────┐
▼ ▼
┌─────────────────┐ ┌──────────────────┐
│ Interpreter │ │ JIT Compiler │
│ Fast startup, │ │ Compiles "hot" │
│ line-by-line │ │ code into native │
│ execution │ │ machine code │
└─────────────────┘ └────────┬─────────┘
│
┌────────┴────────┐
▼ ▼
┌───────────┐ ┌───────────┐
│ C1 (Tier3)│ │ C2 (Tier4)│
│ Client JIT│ │ Server JIT│
└───────────┘ └───────────┘
- Interpreter: Starts executing bytecode immediately line by line with zero compilation delay.
- JIT (Just-In-Time) Compiler:
- Monitors code execution count via profiling counters.
- Identifies "Hot Spots" (loops and frequently called methods).
- Compiles hot bytecode into highly optimized native machine assembly stored in the Code Cache.
- Applies optimizations such as Method Inlining, Dead Code Elimination, and Escape Analysis.