JVM Architecture
Inside the Java Virtual Machine — class loader subsystem, runtime data areas (heap, stacks, metaspace), the execution engine (interpreter, JIT, GC), bytecode and the stack machine, and JVM implementations.
The Java Virtual Machine is an abstract computer specified by the JVM Specification. It loads .class files, verifies them, and executes their bytecode — interpreting it at first and compiling hot code to native machine code. Many languages target it: Java, Kotlin, Scala, Clojure, Groovy.
The big picture
1. Class loader subsystem
Loads classes lazily, the first time they're used, in three phases: loading (find the bytes and create a Class object), linking (verify the bytecode is safe, prepare static fields with default values, resolve symbolic references), and initialisation (run static initialisers). Loaders are hierarchical: bootstrap → platform → application. Details in Class loading.
2. Runtime data areas
| Area | Shared? | Holds | Error when exhausted |
|---|---|---|---|
| Heap | All threads | Every object and array; split into generations/regions by the GC | OutOfMemoryError: Java heap space |
| Metaspace (Java 8+, native memory) | All threads | Class metadata, method bytecode, runtime constant pools | OutOfMemoryError: Metaspace |
| JVM stack | Per thread | One frame per method call | StackOverflowError |
| PC register | Per thread | Address of the current bytecode instruction | — |
| Native method stack | Per thread | Frames for native (C/C++) code | — |
| Code cache | Shared | JIT-compiled machine code | Warning; JIT stops compiling |
Stack frames
Each method call pushes a frame containing:
- a local variable array (parameters and locals; slot 0 is
thisfor instance methods), - an operand stack where bytecode instructions push and pop values,
- a reference to the class's runtime constant pool (for resolving methods and fields).
Primitives and references in locals live in the frame; the objects they refer to are always on the heap (unless the JIT eliminates the allocation — see escape analysis in JIT compilation).
3. Bytecode: a stack machine
The JVM is a stack-based machine: instructions operate on the operand stack instead of registers. This makes bytecode compact and independent of any CPU's register set.
static int add(int a, int b) {
int c = a + b;
return c * 2;
}javap -c Calc.classstatic int add(int, int);
0: iload_0 // push local 0 (a)
1: iload_1 // push local 1 (b)
2: iadd // pop two, push a+b
3: istore_2 // pop into local 2 (c)
4: iload_2 // push c
5: iconst_2 // push constant 2
6: imul // pop two, push c*2
7: ireturn // return top of stackInstruction prefixes indicate types: i int, l long, f float, d double, a reference. Method calls use invokevirtual (normal instance methods, dynamic dispatch), invokestatic, invokespecial (constructors, private, super), invokeinterface, and invokedynamic (lambdas, string concatenation, bootstrap-linked at runtime).
4. Execution engine
- Interpreter: executes bytecode instruction by instruction. Starts instantly, but slow.
- Profiling: counts method invocations and loop iterations, records types seen at call sites and branch frequencies.
- JIT compilers: hot methods are compiled to native code — first by C1 (fast compile, light optimisation), then the hottest by C2 (aggressive, profile-guided optimisation). This is tiered compilation.
- Deoptimisation: if an assumption breaks (a new subclass is loaded, a never-taken branch is taken), compiled code is discarded and execution falls back to the interpreter.
This is why Java programs have a warm-up period, and why micro-benchmarks need JMH. See JIT compilation.
The garbage collector is also part of the execution engine: it reclaims unreachable heap objects (JVM memory & GC).
5. Native interface
JNI (and the newer Foreign Function & Memory API, Java 22) lets Java call native libraries and work with off-heap memory — used by database drivers, compression libraries, ML runtimes.
JVM implementations and distributions
| Implementation | Notes |
|---|---|
| HotSpot (OpenJDK) | The standard JVM in Oracle JDK, Temurin, Corretto, Zulu, Microsoft Build of OpenJDK |
| OpenJ9 (Eclipse) | Lower memory footprint, fast startup with shared class caches |
| GraalVM | Graal JIT written in Java; native-image AOT-compiles to a standalone executable with millisecond startup |
| Android ART | Runs DEX bytecode (converted from class files), AOT + JIT |
Useful JVM tools
| Tool | Purpose |
|---|---|
javap -c -v | Disassemble class files |
jcmd <pid> ... | Swiss-army knife: thread dumps, heap info, GC, flags, JFR |
jstack | Thread dump (diagnose deadlocks and hangs) |
jmap -dump | Heap dump for memory analysis (Eclipse MAT, VisualVM) |
jstat -gc | GC statistics over time |
| Java Flight Recorder + JDK Mission Control | Low-overhead production profiling |
-Xlog:gc* | GC logging |
- The JVM loads, verifies and executes bytecode; many languages target it.
- Heap and metaspace are shared; each thread has its own stack, PC and native stack.
- Bytecode is a stack machine: instructions push and pop an operand stack in each frame.
- Execution starts in the interpreter; hot code is JIT-compiled by C1 then C2, with deoptimisation as a safety net.
- Know the tools: javap, jcmd, jstack, heap dumps, JFR.
QExplain the JVM memory areas.medium
The heap (shared) stores all objects and is managed by the GC. Metaspace (shared, native memory) holds class metadata and bytecode. Each thread has a JVM stack of frames holding local variables and an operand stack, a program counter, and a native method stack. There is also a code cache for JIT-compiled code. Exhausting the heap or metaspace causes OutOfMemoryError; too-deep recursion causes StackOverflowError.
QIs Java interpreted or compiled?easy
Both. javac compiles source to platform-independent bytecode. The JVM initially interprets the bytecode, profiles it, and JIT-compiles frequently executed methods into optimised native machine code at runtime. AOT options like GraalVM native-image can compile ahead of time instead.