KTU OOP Java2024 Scheme • 60M ESE
MODULE 1/Section 1: Introduction to Java

Java Virtual Machine (JVM) Architecture & Complete Execution Process

HIGH PRIORITY (Prep Guide)Official Syllabus~18 min study
Official KTU 2024 Syllabus Topics Covered Here
Java Virtual Machine (JVM)Java execution processJVM architecture

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.

2. Start From Zero: The Automated Theatre Stage

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.

Key Takeaway: JVM has 3 main subsystems: (1) Class Loader Subsystem, (2) Runtime Data Areas (5 memory regions), and (3) Execution Engine (Interpreter + JIT + GC) connected to OS via JNI.

3. Essential Exam Definitions

Class Loader Subsystem
JVM subsystem responsible for dynamically Loading .class files, Linking (Bytecode Verification, Preparation of static memory, Resolution of symbolic references), and Initializing static variables/blocks.
JIT (Just-In-Time) Compiler
Component of the JVM Execution Engine that boosts performance by compiling frequently executed bytecode ('hot spots') directly into native CPU machine code at runtime.
Garbage Collector (GC)
Automatic background memory manager inside the JVM Execution Engine that identifies and reclaims Heap objects no longer referenced by any live variable.

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 .class binary streams via Bootstrap ClassLoader (core java.lang.*), Platform/Extension ClassLoader, and Application ClassLoader (your CLASSPATH).
  • 2. Linking: (a) Bytecode Verification checks stack bounds, type safety, and ensures .class wasn't tampered with; (b) Preparation allocates memory for static fields 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 static variables. (In Java 8+, implemented as Metaspace).
  • Heap Area (Shared): Stores all object instances created with new and 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()/delete bugs.
  • 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. 1. Write source code in .java file and compile using javac to generate .class bytecode.
  2. 2. Launch java MainClass; OS starts JVM instance with initial Heap and Method Area.
  3. 3. Class Loader loads MainClass.class, verifies bytecode, prepares static slots, and runs static initializers.
  4. 4. Main thread is created with its own JVM Stack and PC Register; JVM pushes a frame for main().
  5. 5. Execution Engine executes main() bytecode via Interpreter + JIT while allocating new objects on the Heap.
  6. 6. Garbage Collector cleans unreferenced Heap objects; JVM exits once all non-daemon threads finish.

6. Examination-Ready Diagrams

Diagram 1

JVM Architecture

JAVA VIRTUAL MACHINE (JVM) ARCHITECTUREClass Loader Subsystem1. Loading (Bootstrap, Extension/Platform, Application) • 2. Linking (Verify, Prepare, Resolve) • 3. InitializationRUNTIME DATA AREASMethod AreaClass metadata & static (Shared)Heap AreaAll objects & arrays (Shared)JVM StacksFrames & locals (Per Thread)PC RegistersCurrent instr. (Per Thread)Native StacksC/C++ calls (Per Thread)EXECUTION ENGINEInterpreterLine-by-line bytecodeJIT CompilerHot code -> NativeGarbage CollectorReclaims HeapJNINative InterfaceNative LibsOS C/C++ APIs

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.

Key Components
  • Class Loader Subsystem: Loads, links (verify, prepare, resolve), and initializes .class bytecode 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.
Step-by-Step Flow
  1. Class Loader reads .class files, verifies bytecode safety, allocates static fields, and initializes static blocks.
  2. Class structures go to Method Area; objects created during execution go to Heap.
  3. Each thread receives its own JVM Stack, PC Register, and Native Method Stack.
  4. Execution Engine interprets bytecode line-by-line while JIT compiles hot loops directly to native CPU instructions.
  5. Garbage Collector automatically reclaims unreachable objects from the Heap.
How to Draw in KTU Exam
  • 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.
Diagram 2

Java Program Compilation and Execution

Source CodeProgram.javaJava Compilerjavac Program.javaBytecode (.class)Platform Independent (WORA)Windows JVMInterpreter/JIT -> x64 MachineLinux JVMInterpreter/JIT -> ELF MachinemacOS JVMInterpreter/JIT -> ARM64/x64Compile Time (Once)Runtime (Anywhere)

Two-stage hybrid compilation and interpretation flow demonstrating Write Once, Run Anywhere (WORA) across Windows, Linux, and macOS.

Key Components
  • Source Code (.java): Human-readable Java class file written by the programmer.
  • Java Compiler (javac): Checks syntax/types and translates .java into 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.
Step-by-Step Flow
  1. Write source file e.g. HelloWorld.java.
  2. Run javac HelloWorld.java to compile source into HelloWorld.class (Bytecode).
  3. Run java HelloWorld to launch the OS-specific JVM.
  4. JVM loads bytecode, verifies it, and translates it via Interpreter + JIT into host CPU machine code.
How to Draw in KTU Exam
  • 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 .class clearly as 'Platform Independent Bytecode (WORA)'.

7. Complete Java Code Examples & Tracing

Observing Method Area, Stack, Heap & Static Loading OrderJvmMemoryTrace.java
Demonstrates how static fields initialize during Class Loading (Method Area), local primitives live on Stack Frames, and objects live on Heap.
1public 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}
Expected Output
1. Static block runs during Class Loading. classCounter = 100
2. main() started on JVM Stack.
3. Stack total = 25, Heap objectId = 5
Keywords:staticnewvoidHeapStackMethod Area
Common Exam & Coding Mistakes
  • Assuming main() always executes before static blocks (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 .class files 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 new objects 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.
Exam Diagram Note: Reproduce Diagram 1 (JVM Architecture) with Class Loader on top, 5 Runtime Data Areas in middle, and Execution Engine + JNI at bottom.

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?