The JVM works by loading compiled Java bytecode, verifying and preparing it, and then executing it on an abstract stack-based machine that translates or compiles that bytecode into native instructions for the host platform; its architecture divides into a class loader subsystem, runtime data areas (method area, heap, Java stacks, PC registers, and native method stacks), an execution engine with interpreter and JIT compiler, and a native method interface plus native method libraries. The JVM is a specification, not a single program: different vendors (HotSpot/OpenJDK, Eclipse OpenJ9, GraalVM, and others) implement it, and editions differ in internals such as garbage collectors, JIT strategies, and class-file versions, so exact behavior depends on the implementation and Java release you are running.
What "the JVM" Actually Is
The Java Virtual Machine is an abstract computing machine defined by the Java Virtual Machine Specification (part of the Java SE specification). Java source compiles to platform-independent bytecode (.class files), and the JVM executes that bytecode. The well-known slogan "write once, run anywhere" rests on this: the compiler targets the JVM's instruction set, and each platform supplies a JVM implementation for its operating system and CPU architecture.
Two clarifications prevent most confusion:
1: The JVM is not the Java language. It executes bytecode, which other languages (Kotlin, Scala, Groovy, Clojure) also compile to. The JVM knows nothing about Java syntax. 2: The JVM is not one binary. "The JVM" in everyday speech usually means the HotSpot implementation shipped in OpenJDK/Oracle JDK, but the specification permits different internal designs. The abstract architecture below is specified; the implementation details (for example, whether the interpreter uses a switch-based loop or template assembly, or which collector runs by default) are vendor- and version-specific.
Java releases since 2017 follow a six-month cadence with long-term-support releases roughly every two years. Given the reference date, current long-term-support lines include Java 17 and Java 21, with newer feature releases in between, but you should confirm the exact version in use because collector defaults, class-file versions, and experimental features change between releases.
The Big Picture: From Source to Execution
A typical path looks like this:
1: javac compiles .java source into .class files containing bytecode and a constant pool. 2: At runtime, the JVM's class loader subsystem locates, loads, links, and initializes those classes. 3: The execution engine interprets or JIT-compiles bytecode into native code. 4: Runtime data areas hold objects, class metadata, and per-thread execution state. 5: When code calls native libraries (I/O, java.lang.invoke internals, Unsafe), the native method interface bridges into platform code.
The JVM itself is typically a native process (written largely in C++ in HotSpot) that hosts this machinery. Your Java program is data plus bytecode interpreted/compiled inside that process.
Architecture Overview
The specified runtime structure has these major parts. The exact internal representation (for example, whether the method area lives inside the heap) is implementation-defined.
| Subsystem / Area | Role |
|---|---|
| Class loader subsystem | Loading, linking (verification, preparation, resolution), initialization |
| Method area | Per-class metadata: runtime constant pool, field/method data, static variables |
| Heap | All class instances and arrays; managed by garbage collection |
| Java stacks (one per thread) | Frames for Java method invocations: locals, operand stack, frame data |
| PC (program counter) register | Per-thread address of the currently executing bytecode instruction |
| Native method stacks | Frames for native (non-Java) method calls |
| Execution engine | Interpreter, JIT compiler(s), garbage collector coordination |
| Native method interface (JNI) | Bridge between JVM and native code |
| Native method libraries | Platform libraries the JVM and your native calls use |
An important structural fact: the heap and method area are shared across threads; Java stacks, PC registers, and native method stacks are per-thread. This split explains why object visibility across threads requires synchronization (the Java Memory Model) while each thread's local execution state is private by construction.
Class Loading: Getting Bytecode Into the Machine
Loading is not a single step. The specification distinguishes:
Loading: the class loader finds the binary representation (usually a .class file or bytes from a JAR, module, or generated source) and creates the class in the JVM. Linking, which has three sub-steps: Verification: checks structural and semantic correctness of the bytecode (for example, that operand stack types match instructions and that no illegal access occurs) to protect the JVM's integrity. Preparation: allocates static fields and sets them to default values (zero, false, null) — not yet to their explicit initializers. Resolution: converts symbolic references in the constant pool (names) into direct references, often lazily. Initialization: runs class initializers (, i.e., static initializers and static field assignments) in a specified order, with locking to ensure it happens once per class per loader.
The parent-delegation model and class identity
The classic built-in loaders — bootstrap (or "primordial"), platform/extensions (terminology changed across releases; older documents say "extension" loader), and application/system loader — follow parent delegation: a loader asks its parent first, and only loads if the parent cannot. A custom loader can override this, and frameworks sometimes do so deliberately (for example, application servers isolating deployed applications).
A subtlety that trips people up: a class's identity includes its defining loader. The same class bytes loaded by two different loaders produce two distinct runtime classes, and casts between them fail with ClassCastException. This is why "classpath hell" and plugin-isolation bugs occur. Also, note the phrase "runtime constant pool" in the method area: it holds resolved constants and symbolic references per class.
Runtime Data Areas in Detail
Method area
The method area stores per-class structures: the runtime constant pool, field and method descriptors, method bytecode, and static variables. In HotSpot historically this was the "permanent generation," later replaced by Metaspace (since Java 8), which stores class metadata in native memory rather than the Java heap. The distinction matters operationally: -XX:MaxMetaspaceSize and related flags govern native-memory class metadata, while heap flags govern objects. Leaking class loaders (for example, redeploying applications in a server that retains references) shows up as Metaspace growth.
Heap
The heap holds all class instances and arrays. It is created at JVM startup and shared by all threads. Garbage collection manages it; collectors differ substantially (serial, parallel, G1, ZGC, Shenandoah are HotSpot examples, with availability depending on release and vendor). Since Java 9's module system and later changes, defaults shifted: G1 became the default collector in Java 9, and later releases introduced generational ZGC as an option (Java 21). Do not treat any default as timeless — confirm for your release.
Java stacks and frames
Each thread has a private Java stack holding frames, one per Java method invocation. A frame contains:
Local variables: slots for method parameters, this, and declared locals (long/double occupy two slots in the classic frame model). Operand stack: a LIFO stack for intermediate values; bytecode instructions push and pop operands. This is what "stack-based" means for the JVM's instruction set. Frame data: references to the constant pool, return addresses, and exception-handling information.
Stack depth is limited (-Xss in HotSpot sets per-thread stack size). Deep or unbounded recursion triggers StackOverflowError; note that this is an Error thrown by the JVM's stack machinery, distinct from OutOfMemoryError for heap exhaustion.
PC register and native method stacks
Conceptionally, each thread has a PC register pointing at the current bytecode instruction; during native execution the value is undefined. Native method stacks hold frames for native calls (for JNI or JVM-internal native routines).
A high-value distinction: the operand stack of a frame is not the Java stack you reason about in source code, and local variables are not necessarily in memory — the JIT may keep them in registers or eliminate them entirely after analysis. The specification describes an abstract machine; optimizations preserve observable behavior (as constrained by the Java Memory Model and language semantics).
The Execution Engine: Interpreter and JIT
Interpretation
The interpreter executes bytecode one instruction at a time. This lets code start almost immediately (no compilation wait) and keeps the system simple, but it is slower than native code for hot loops. Implementations vary: some use switch dispatch, others generate small template routines.
Just-in-time compilation
HotSpot's engine additionally profile-guides compilation: methods start interpreted, and once they become "hot" (invocation and back-edge counters exceed thresholds), the JIT compiles them, often at two tiers:
C1 (client compiler): fast compilation, moderate optimization. C2 (server compiler): slower compilation, aggressive optimization (inlining, escape analysis, loop optimizations).
The tiered compilation policy combines both; flags like -XX:+PrintCompilation (diagnostic) and -XX:TieredStopAtLevel expose behavior but are implementation details, not portability guarantees.
Deoptimization and speculation
JIT compilers speculate: they may inline a virtual call based on observed receiver types. If a later call violates the assumption, the machine deoptimizes — it reconstructs the interpreter state and continues in bytecode. This is why "the JIT compiles the code" never means "the bytecode is discarded"; the interpreter state is the safety net. A practical consequence: benchmark results and stack traces can be affected by compilation/deoptimization timing, and warm-up matters.
Garbage collection interaction
The execution engine coordinates with the collector through safepoints and barriers. Collectors may stop-the-world, run concurrently, or both; the choice affects pause behavior. The Java Language Specification does not mandate a collector, and the JVM specification does not dictate one either — collector behavior and flags are implementation- and version-specific. Practical guidance: choose based on measured latency/throughput needs for your JDK, not on folklore.
The Native Interface and Libraries
The JVM does not do everything itself. The Java Native Interface (JNI) allows Java code to call native code and vice versa. Native methods are declared in Java, resolved by the JVM, and executed on native method stacks. Historical note: older interfaces (Netscape's JRI, Microsoft's RNI) existed, but JNI became the standard; it remains powerful and error-prone (manual reference management, undefined behavior on misuse). The Foreign Function & Memory API (finalized in Java 22) provides a safer, supported alternative to many JNI use cases, though it is not a drop-in replacement for every scenario — check the release you target.
The "native method libraries" component is simply the platform libraries (C runtime, OS services) the JVM and JNI code link against.
Verification and Security Boundaries
Bytecode verification is central: it ensures that a class file, even if maliciously crafted, cannot corrupt the JVM (for example, by treating an integer as a reference). The verifier and linker implement access control against the class loader and module system rules. Since Java 9, the module system (JPMS) adds readability rules and stronger encapsulation, and the class-file format gains new attributes and versions over time. Class-file major versions are tied to compiler releases (for example, Java 17 produces version 61, Java 21 version 65), and a JVM refuses class files newer than it supports with UnsupportedClassVersionError. This is a common deployment failure: compiling with a newer JDK than the runtime.
Execution Example: A Trace Through the Architecture
Consider:
Walking the architecture:
1: The application loader locates Trace.class (parent delegation first). 2: Verification checks the bytecode; preparation sets static fields (none here) to defaults; initialization runs static initializers (none here besides implicit setup). 3: main executes on a new Java stack frame: local slots and an operand stack. 4: In the loop, add is a static call producing a new frame; its operands are pushed, iadd pops/pushes, and the result returns to the caller's operand stack. 5: After warm-up, the JIT may inline add and optimize the loop — observable behavior is unchanged, but the abstract frames may no longer literally exist. 6: println resolves through the constant pool to a method on System.out and ultimately calls native I/O code via JNI/native libraries.
A good mental check: the constant pool explains how names become references; the operand stack explains why iadd needs no registers; the per-thread stack explains why recursion depth maps to -Xss.
Where the Model Stops Applying
The specified architecture is abstract. Implementation choices differ in ways that matter:
HotSpot's Metaspace versus earlier permanent generation; other vendors store metadata differently. Stack frame layout, whether the interpreter uses computed goto, and JIT tier names are not portable facts. Whether the method area is part of the heap is implementation-defined, and the specification says so explicitly. The JVM specification describes required class-file syntax and verification constraints, but tools like -Xverify:none (older releases) or newer verification controls are implementation-specific.
Do not universalize local tuning advice: -Xmx, -Xss, collector flags, and GC defaults are vendor- and release-specific, and they may change with a JDK upgrade. Measure on the actual runtime you deploy.
Common Confusions, Cleared Up
"The JVM compiles Java to machine code." Only sometimes and only via JIT; it always begins by interpreting bytecode, and compilation is selective. "Bytecode is assembly for the JVM." It is a binary instruction set for an abstract stack machine, with verification rules; calling it "assembly" is loose analogy. "The heap is managed, so memory leaks are impossible." False: reachable-but-unneeded objects (static collections, listeners, class loader leaks) leak in any managed heap; Metaspace can also grow. "StackOverflowError means the heap is full." No — it is stack exhaustion (often recursion); OutOfMemoryError covers heap/metaspace/native exhaustion with different causes. "The JIT makes all code fast." Compilation costs, deoptimization, profiling overhead, and code-cache limits mean not all code benefits equally; startup-sensitive workloads often favor less aggressive compilation (implementations differ).
Practical Takeaways
Understand the flow — load, link, initialize, then interpret/JIT — and the memory split between shared (heap, method area) and per-thread (stacks, PC) structures. Use the abstract model to reason about class loading identity, verification failures (UnsupportedClassVersionError, NoClassDefFoundError caused by init/link failures), stack depth, and memory pressure (heap versus Metaspace). Treat collector and JIT flags as version-specific tuning, verify against your JDK's documentation, and expect implementations to differ where the specification is silent. For authoritative specifics on class-file versions, module rules, and required runtime behavior, consult the JVM Specification for the Java SE release you target; for collector and JIT internals, consult the documentation for your specific JDK distribution and version, because those details are where "how the JVM works" stops being portable and becomes implementation fact.