How JVM Works (Beginner Guide)

 

How JVM Works (Beginner Guide)

From Writing a Java File → Compile → Run → Memory Allocation → Program End

(With Full JVM Architecture Explained Step-by-Step)

Java does not run directly on your computer.
Instead, it runs inside a powerful engine called the Java Virtual Machine (JVM).

Let’s walk through every step, starting from writing a Java program to executing it inside the JVM, using this architecture:

┌──────────────────────────────┐ │ Java Virtual Machine │ ├──────────────────────────────┤ │ Class Loader Subsystem │ ├──────────────────────────────┤ │ Runtime Data Areas │ │ ├─ Method Area │ │ ├─ Heap │ │ ├─ Java Stack │ │ ├─ PC Registers │ │ └─ Native Method Stack │ ├──────────────────────────────┤ │ Execution Engine │ │ ├─ Interpreter │ │ ├─ JIT Compiler │ │ └─ Garbage Collector │ ├──────────────────────────────┤ │ Native Interface (JNI) │ └──────────────────────────────┘

Let’s go step by step.


1. You Create a Java File (Hello.java)

Example:

public class Hello { public static void main(String[] args) { int x = 10; int y = 20; int sum = x + y; System.out.println("Sum: " + sum); } }

What exists now?

  • A plain text file: Hello.java

  • Stored on disk, not in JVM

  • No memory allocation yet

  • JVM has not started yet


2. You Compile the Program (javac Hello.java)

What happens?

✔ Step 1: Java Compiler reads .java file (outside JVM)

  • Checks syntax

  • Converts code to bytecode

  • Produces a new file: Hello.class

✔ Step 2: Hello.class contains bytecode

This is platform-independent, readable by any JVM.

📍 JVM still not running

Memory is still not allocated.


3. You Run the Program (java Hello)

Now JVM starts.

This is where your architecture comes alive.


Stage 1 — Class Loader Subsystem Loads Your Program

When you run:

java Hello

Class Loader Subsystem loads:

  1. Hello.class

  2. Object.class

  3. String.class

  4. System classes from JDK

✔ Where does class metadata go?

➡️ Method Area

Method Area: ├─ Class name (Hello) ├─ Methods (main, constructors) ├─ Fields (none static here) ├─ Constant pool

Stage 2 — Runtime Data Areas Are Created

JVM now prepares memory to run your program.

Runtime Data Areas ├─ Method Areaclass definitions ├─ Heapobjects, arrays ├─ Java Stacklocal variables ├─ PC Registerscurrent instruction of each thread └─ Native Method StackC/C++ calls

Stage 3 — JVM Starts main() Method

JVM creates a new thread called:

main thread

4. JVM Creates a Java Stack for the Main Thread

Each thread gets its own Java Stack.

The stack stores:

  • Method calls

  • Local variables

  • Intermediate calculations

For our program:

Stack Frame for main(): x = ? y = ? sum = ?

5. JVM Executes Bytecode Using the Execution Engine

Execution Engine is the brain:

✔ Interpreter

Executes bytecode line by line.

✔ JIT Compiler

Optimizes frequently run code → machine code
Makes the program faster.

✔ PC Register (Program Counter)

Keeps track of:

Which instruction is running now?

Each thread has a PC Register.


6. JVM Executes Each Line (Memory Flow)

Let’s walk through your code line-by-line and see what happens in each memory area.


🧮 Line 1: int x = 10;

✔ Where is x stored?

➡️ Java Stack (inside main thread stack frame)

Java Stack: x = 10

🧮 Line 2: int y = 20;

Java Stack: x = 10 y = 20

🧮 Line 3: int sum = x + y;

Execution Engine:

  • Reads x from stack

  • Reads y from stack

  • Performs addition using CPU

  • Stores result in stack

Java Stack: x = 10 y = 20 sum = 30

✔ No Heap usage yet
✔ No Garbage Collector activity yet


📝 Line 4: Printing Value

System.out.println("Sum: " + sum);

What happens internally?

  1. JVM creates String "Sum: " → stored in String Pool (Heap — Method Area-backed)

  2. JVM creates a new concatenated String → "Sum: 30" → Heap

  3. println() is a native method → uses Native Method Stack via JNI

Heap: "Sum: " "30" "Sum: 30"

7. Garbage Collector Works (If Needed)

GC checks unused objects in Heap:

  • Temporary strings

  • Temporary objects created in expression evaluation

If no references → GC removes them.

Stack cleanup happens automatically when method returns.


8. End of main() Method

When main() finishes:

  1. Stack frame for main is removed

  2. Local variables (x, y, sum) are gone

  3. Heap objects referenced by nothing → eligible for GC

  4. PC Register for main thread resets

  5. Main thread dies

  6. JVM shuts down (if no other threads exist)


Full Summary with Memory Flow Diagram

Step 1: Write Hello.javaOn Disk Step 2: Compile (javac)Hello.class (bytecode) on Disk Step 3: Run (java Hello)JVM Starts ┌──────────────────────────────────────────────────────────┐ │ JVM Execution Flow │ ├──────────────────────────────────────────────────────────┤ │ Class Loader Subsystem │ │ → Loads Hello.class │ │ → Loads java.lang classes │ ├──────────────────────────────────────────────────────────┤ │ Runtime Data Areas │ │ Method AreaClass definitions │ │ HeapObjects, Strings │ │ Java StackLocal variables, method frames │ │ PC RegisterCurrent instruction │ │ Native StackNative calls │ ├──────────────────────────────────────────────────────────┤ │ Execution Engine │ │ Interpreterexecutes bytecode │ │ JIToptimizes hot code │ │ GCcleans unused heap objects │ └──────────────────────────────────────────────────────────┘ At runtime: Java Stack: x = 10 y = 20 sum = 30 Heap: "Sum: 30" End: - Stack destroyed - Heap cleaned by GC - JVM shutdown

One-Line Takeaway

Stack = method + variables, Heap = objects, Method Area = class info, Execution Engine = runs bytecode, GC = cleans heap, PC Register = tracks instruction.

No comments:

Post a Comment

How JVM Identifies and Communicates Between Stack, Heap, and Method Area

  How JVM Identifies and Communicates Between Stack, Heap, and Method Area Java has three types of memory involved during execution: 1️⃣ ...

Featured Posts