On this page
JVM ClassLoading, Bytecode & HotSpot JIT
Prev: OpenJDK Distributions & Toolchain | Up: JVM & OpenJDK | Next: Garbage Collection (G1 & ZGC)
Rel: Compilation, Interpretation & JIT, Diagnostics, JFR & jcmd, Generics & Type Erasure
Ref: Java Virtual Machine Specification (SE 21) , OpenJDK HotSpot Group
Tags: #public #java #jvm #openjdk #jit #bytecode #tier/standard
Overview
Java achieves portability and high steady-state throughput through a two-stage execution model: javac compiles source code into platform-independent JVM bytecode (.class files), and OpenJDK's HotSpot JVM dynamically loads, verifies, interprets, profiles, and just-in-time (JIT) compiles hot paths into native machine code.
Class Loading Lifecycle
Classes are loaded lazily on first active use through three phases:
- Loading: A
ClassLoaderlocates the.classbinary stream and creates ajava.lang.Classobject in Metaspace. Built-in hierarchy uses parent delegation:- Bootstrap ClassLoader (native, loads
java.baseand core JDK modules) - Platform ClassLoader (loads standard non-base JDK modules)
- Application (System) ClassLoader (loads your classpath / module path)
- Bootstrap ClassLoader (native, loads
- Linking:
- Verification: Ensures bytecode obeys stack-depth, type-safety, and control-flow rules.
- Preparation: Allocates static fields with default zero/null values.
- Resolution: Resolves symbolic constant-pool references to direct memory references.
- Initialization: Executes static field initializers and
static { ... }blocks once, thread-safely, on first active use.
Stack-Based Bytecode
The JVM is a stack-based virtual machine. Each method frame holds a local variable array and an operand stack. You can inspect generated bytecode with javap -c:
static int addOne(int x) {
return x + 1;
}
0: iload_0 // push local int slot 0 (x) onto operand stack
1: iconst_1 // push int constant 1
2: iadd // pop two ints, push sum
3: ireturn // return top int
HotSpot Tiered Compilation (Interpreter + C1 + C2)
HotSpot balances fast startup with peak native speed using tiered compilation:
| Tier | Engine | Behavior |
|---|---|---|
| Tier 0 | Interpreter | Executes bytecode immediately; collects basic call/loop counters |
| Tier 1–3 | C1 (Client compiler) | Compiles quickly with light optimizations and rich profiling counters (branch frequencies, receiver types) |
| Tier 4 | C2 (Server compiler) | Aggressively compiles hot methods using live profile data |
Key C2 Optimizations
- Method inlining: Eliminates call overhead and unlocks downstream optimizations across method boundaries.
- Devirtualization & guarded inline caches: Turns virtual/interface calls into direct jumps when profiling shows only 1–2 concrete receiver types (monomorphic/bimorphic sites).
- Escape analysis & scalar replacement: If an allocated object never escapes a method, C2 can eliminate the heap allocation entirely and keep its fields in CPU registers.
- Deoptimization: If a speculative assumption is later invalidated (e.g., a new subclass is loaded), HotSpot safely falls back from compiled code to the interpreter and recompiles.
Startup & Memory Regions
- Heap: Stores all object instances and arrays; managed by the garbage collector.
- Metaspace: Native memory holding class metadata and constant pools (replaced PermGen in Java 8).
- Code Cache: Native memory holding JIT-compiled machine code.
- Thread Stacks: One native stack per platform thread (or heap-mounted continuations for virtual threads).
Linked from
- tuts/java/jvm_and_openjdk
- tuts/java/jvm_and_openjdk/diagnostics_jfr_and_jcmd
- tuts/java/jvm_and_openjdk/garbage_collection_g1_and_zgc
- tuts/java/jvm_and_openjdk/openjdk_distributions_and_toolchain
- tuts/java/language_and_oop/generics_and_type_erasure
- tuts/programming/memory_and_execution/compilation_interpretation_and_jit
Related notes
- tuts/java/jvm_and_openjdk/garbage_collection_g1_and_zgc Linked here; links to this page; shared tags: #java, #jvm, #openjdk, #tier
- tuts/java/jvm_and_openjdk Linked here; links to this page; shared tags: #java, #jvm, #openjdk
- tuts/java/jvm_and_openjdk/diagnostics_jfr_and_jcmd Linked here; links to this page; shared tags: #java, #openjdk, #tier
- tuts/java/jvm_and_openjdk/openjdk_distributions_and_toolchain Linked here; links to this page; shared tags: #java, #openjdk, #tier
- tuts/java/language_and_oop/generics_and_type_erasure Linked here; links to this page; shared tags: #java, #tier
- tuts/programming/memory_and_execution/compilation_interpretation_and_jit Linked here; links to this page
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #openjdk, #tier
- tuts/java shared tags: #java, #jvm, #openjdk
- tuts/java/builtins/AutoCloseable shared tags: #java, #tier
- tuts/java/builtins/enum shared tags: #java, #tier