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.

Intermediate 4 min readLesson 8 of 14 Interview Q&A

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

class data.class files / JARsClass loader subsystemload → link → initialiseRuntime data areasHeapobjects (shared)Metaspaceclass metadata (shared)Thread stacksframes (per thread)PC registers · native stacksper threadExecution engineinterpreter · JIT (C1/C2) · GCJNI / FFMnative libraries

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

AreaShared?HoldsError when exhausted
HeapAll threadsEvery object and array; split into generations/regions by the GCOutOfMemoryError: Java heap space
Metaspace (Java 8+, native memory)All threadsClass metadata, method bytecode, runtime constant poolsOutOfMemoryError: Metaspace
JVM stackPer threadOne frame per method callStackOverflowError
PC registerPer threadAddress of the current bytecode instruction—
Native method stackPer threadFrames for native (C/C++) code—
Code cacheSharedJIT-compiled machine codeWarning; JIT stops compiling

Stack frames

Each method call pushes a frame containing:

  • a local variable array (parameters and locals; slot 0 is this for 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.

Java
static int add(int a, int b) {
    int c = a + b;
    return c * 2;
}
Shell
javap -c Calc.class
Assembly
static 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 stack

Instruction 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).

→ iadd → 7← topb = 4a = 3
Operand stack during iadd: two values popped, one pushed.

4. Execution engine

  1. Interpreter: executes bytecode instruction by instruction. Starts instantly, but slow.
  2. Profiling: counts method invocations and loop iterations, records types seen at call sites and branch frequencies.
  3. 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.
  4. 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

ImplementationNotes
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
GraalVMGraal JIT written in Java; native-image AOT-compiles to a standalone executable with millisecond startup
Android ARTRuns DEX bytecode (converted from class files), AOT + JIT

Useful JVM tools

ToolPurpose
javap -c -vDisassemble class files
jcmd <pid> ...Swiss-army knife: thread dumps, heap info, GC, flags, JFR
jstackThread dump (diagnose deadlocks and hangs)
jmap -dumpHeap dump for memory analysis (Eclipse MAT, VisualVM)
jstat -gcGC statistics over time
Java Flight Recorder + JDK Mission ControlLow-overhead production profiling
-Xlog:gc*GC logging
Key takeaways
  • 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.
Interview questions
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.