Java Virtual Machine (JVM) Architecture & Complete Execution Process
1. Topic Introduction
The Java Virtual Machine (JVM) is the heart of Java's portability, memory safety, and performance. In KTU examinations, 'Draw and explain the architecture of JVM' is a cornerstone 6-mark or 9-mark Part B question. Knowing the three major subsystems—Class Loader, Runtime Data Areas, and Execution Engine—gives you full clarity on how Java actually executes in memory.
Think of running a Java program like staging a play. First, the Stage Crew (Class Loader) checks the script (Bytecode Verifier) to ensure no unsafe stunts are written and brings props onto the stage. Second, the Stage Layout (Runtime Data Areas) organizes shared props (Heap & Method Area) and gives each actor their own script notebook (JVM Stack & PC Register). Finally, the Director & Translators (Execution Engine: Interpreter + JIT) read instructions aloud for the actors while the Janitor (Garbage Collector) clears away props no actor uses anymore.
3. Essential Exam Definitions
.class files, Linking (Bytecode Verification, Preparation of static memory, Resolution of symbolic references), and Initializing static variables/blocks.4. Detailed Technical Explanation & Syntax
Subsystem 1: Class Loader Subsystem
Java loads classes dynamically on demand rather than all at once. The Class Loader performs three phases:
- 1. Loading: Reads
.classbinary streams via Bootstrap ClassLoader (corejava.lang.*), Platform/Extension ClassLoader, and Application ClassLoader (yourCLASSPATH). - 2. Linking: (a) Bytecode Verification checks stack bounds, type safety, and ensures
.classwasn't tampered with; (b) Preparation allocates memory forstaticfields with default zero/null values; (c) Resolution replaces symbolic names with direct memory references. - 3. Initialization: Assigns actual initial values to static variables and executes
static { ... }blocks in top-down order.
Subsystem 2: Runtime Data Areas (5 Memory Regions)
The JVM partitions memory into five distinct areas—two shared across all threads, and three created privately per thread:
- Method Area (Shared): Stores class-level metadata, method bytecode, runtime constant pool, and
staticvariables. (In Java 8+, implemented as Metaspace). - Heap Area (Shared): Stores all object instances created with
newand all arrays. This is the primary target of the Garbage Collector. - JVM Stacks (Per Thread): Every thread gets a private stack holding Stack Frames (one per active method call). Each frame holds local variables, operand stack, and return address. Exceeding stack depth throws
StackOverflowError. - PC (Program Counter) Registers (Per Thread): Stores the memory address of the current JVM instruction being executed by that thread.
- Native Method Stacks (Per Thread): Supports native non-Java methods (typically C/C++) invoked via JNI.
Subsystem 3: Execution Engine & JNI
Once bytecode is in Runtime Data Areas, the Execution Engine runs it using three components, supported by JNI:
- Interpreter: Reads and executes bytecode instructions sequentially. Fast startup, but slow if a loop runs thousands of times because the same instruction gets re-interpreted repeatedly.
- JIT Compiler (Just-In-Time): Detects repeatedly called methods/loops ('hot spots') and compiles them once into native CPU machine code so subsequent runs execute directly on hardware.
- Garbage Collector: Frees unreachable objects on the Heap automatically, eliminating manual
free()/deletebugs. - Java Native Interface (JNI) & Native Method Libraries: Bridge allowing Java bytecode to call OS-level C/C++ libraries (e.g., for file I/O or sockets).
5. Working Principle: End-to-End Java Execution Process
- 1. Write source code in
.javafile and compile usingjavacto generate.classbytecode. - 2. Launch
java MainClass; OS starts JVM instance with initial Heap and Method Area. - 3. Class Loader loads
MainClass.class, verifies bytecode, prepares static slots, and runsstaticinitializers. - 4. Main thread is created with its own JVM Stack and PC Register; JVM pushes a frame for
main(). - 5. Execution Engine executes
main()bytecode via Interpreter + JIT while allocatingnewobjects on the Heap. - 6. Garbage Collector cleans unreferenced Heap objects; JVM exits once all non-daemon threads finish.
6. Examination-Ready Diagrams
JVM Architecture
Internal architecture of the Java Virtual Machine showing how compiled .class bytecode is loaded, stored across Runtime Data Areas, and executed by the Execution Engine.
- Class Loader Subsystem: Loads, links (verify, prepare, resolve), and initializes
.classbytecode files. - Method Area: Stores class metadata, static variables, constant pool, and method bytecode (shared).
- Heap Area: Stores all dynamically created objects (
new) and arrays (shared across threads, managed by GC). - JVM Stacks: Per-thread stacks storing stack frames (local variables, operand stack, method return state).
- PC Registers: Holds the address of the currently executing JVM instruction for each thread.
- Native Method Stacks: Supports execution of native (non-Java, e.g., C/C++) methods via JNI.
- Execution Engine: Interpreter, JIT Compiler (hot bytecode to native code), and Garbage Collector (GC).
- JNI & Native Libraries: Bridges JVM with OS-specific native libraries.
- Class Loader reads
.classfiles, verifies bytecode safety, allocates static fields, and initializes static blocks. - Class structures go to Method Area; objects created during execution go to Heap.
- Each thread receives its own JVM Stack, PC Register, and Native Method Stack.
- Execution Engine interprets bytecode line-by-line while JIT compiles hot loops directly to native CPU instructions.
- Garbage Collector automatically reclaims unreachable objects from the Heap.
- Draw three main horizontal/stacked blocks: (1) Class Loader Subsystem at top, (2) Runtime Data Areas in middle (5 clear sub-boxes: Method Area, Heap, JVM Stacks, PC Registers, Native Method Stacks), (3) Execution Engine + JNI + Native Method Libraries at bottom.
- Explicitly mark Method Area & Heap as 'Shared among threads' and Stacks/PC Registers as 'Per Thread'.
- Inside Execution Engine, label Interpreter, JIT Compiler, and Garbage Collector.
Java Program Compilation and Execution
Two-stage hybrid compilation and interpretation flow demonstrating Write Once, Run Anywhere (WORA) across Windows, Linux, and macOS.
- Source Code (.java): Human-readable Java class file written by the programmer.
- Java Compiler (javac): Checks syntax/types and translates
.javainto platform-independent bytecode. - Bytecode (.class): Intermediate binary instructions executed by any JVM.
- Platform-Specific JVM: Windows JVM, Linux JVM, or macOS JVM interprets/JIT-compiles bytecode into native machine code.
- Write source file e.g.
HelloWorld.java. - Run
javac HelloWorld.javato compile source intoHelloWorld.class(Bytecode). - Run
java HelloWorldto launch the OS-specific JVM. - JVM loads bytecode, verifies it, and translates it via Interpreter + JIT into host CPU machine code.
- Use a clean left-to-right pipeline:
.java->[javac Compiler]->.class (Bytecode). - Branch
.class (Bytecode)into 3 boxes:JVM (Windows),JVM (Linux),JVM (macOS)->Machine Code. - Label
.classclearly as 'Platform Independent Bytecode (WORA)'.
7. Complete Java Code Examples & Tracing
| 1 | public class JvmMemoryTrace { |
| 2 | // Stored in Method Area (Class-level) during Class Initialization |
| 3 | static int classCounter = 100; |
| 4 | |
| 5 | // Stored inside each object on the Heap |
| 6 | int objectId; |
| 7 | |
| 8 | static { |
| 9 | System.out.println("1. Static block runs during Class Loading. classCounter = " + classCounter); |
| 10 | } |
| 11 | |
| 12 | JvmMemoryTrace(int id) { |
| 13 | this.objectId = id; |
| 14 | } |
| 15 | |
| 16 | void printDetails(int localBonus) { |
| 17 | int total = objectId + localBonus; // local primitives on JVM Stack frame |
| 18 | System.out.println("3. Stack total = " + total + ", Heap objectId = " + objectId); |
| 19 | } |
| 20 | |
| 21 | public static void main(String[] args) { |
| 22 | System.out.println("2. main() started on JVM Stack."); |
| 23 | JvmMemoryTrace obj = new JvmMemoryTrace(5); // 'obj' ref on Stack, object on Heap |
| 24 | obj.printDetails(20); |
| 25 | } |
| 26 | } |
1. Static block runs during Class Loading. classCounter = 100 2. main() started on JVM Stack. 3. Stack total = 25, Heap objectId = 5
staticnewvoidHeapStackMethod Area- Assuming
main()always executes beforestaticblocks (static initialization runs first when the class loads!). - Stating objects are allocated on the Stack instead of the Heap.
Real-World Applications
- Server tuning in enterprise Java relies on sizing Heap memory (
-Xmx) and selecting Garbage Collectors. - JIT profiling allows long-running Java backend microservices to reach C++-comparable throughput after warm-up.
Important KTU Exam Points
- Draw ALL 3 blocks cleanly in the JVM Architecture diagram: Class Loader, 5 Runtime Data Areas, Execution Engine + JNI.
- Explicitly distinguish Shared Areas (Method Area, Heap) vs Per-Thread Areas (JVM Stack, PC Register, Native Method Stack).
- Explain WHY Java has BOTH an Interpreter and a JIT Compiler (fast startup + native speed for loops).
Memory Techniques & Revision
- 3 ClassLoader Steps — 'L-L-I': Load, Link (Verify-Prepare-Resolve), Initialize.
- 5 Memory Areas — '2 Shared, 3 Personal': Shared = Method + Heap; Personal = JVM Stack + PC Register + Native Stack.
Common Pitfalls to Avoid
- Forgetting the Bytecode Verifier inside the Class Loader Linking stage.
- Missing the PC Register or Native Method Stack among the 5 Runtime Data Areas.
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: The Java Virtual Machine (JVM) is an abstract computing machine that loads, verifies, manages memory for, and executes compiled .class bytecode.
- 1. Class Loader Subsystem: Dynamically loads
.classfiles via Bootstrap, Platform, and Application class loaders; performs Linking (Verify bytecode safety, Prepare static defaults, Resolve symbolic references) and Initialization (execute static blocks). - 2. Runtime Data Areas (Memory): (a) Method Area (shared): stores class metadata, constant pool, and static fields; (b) Heap (shared): stores all
newobjects and arrays, managed by GC; (c) JVM Stacks (per thread): stores method frames and local variables; (d) PC Registers (per thread): tracks current instruction address; (e) Native Method Stacks (per thread): supports C/C++ native calls. - 3. Execution Engine: Interpreter executes bytecode line-by-line; JIT Compiler compiles hot bytecode loops to native machine code for speed; Garbage Collector reclaims unreferenced Heap objects.
- 4. JNI & Native Libraries: Java Native Interface links JVM execution with underlying OS native APIs.
Conclusion: Through this layered subsystem design, the JVM guarantees Java's WORA portability, memory safety via GC, and near-native performance via JIT.
Quick Understanding Check
1. Where are objects created using the new keyword stored inside JVM memory?
2. Which two Runtime Data Areas are shared across all threads in a JVM?