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:
Let’s go step by step.
1. You Create a Java File (Hello.java)
Example:
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:
Class Loader Subsystem loads:
-
Hello.class -
Object.class -
String.class -
System classes from JDK
✔ Where does class metadata go?
➡️ Method Area
Stage 2 — Runtime Data Areas Are Created
JVM now prepares memory to run your program.
Stage 3 — JVM Starts main() Method
JVM creates a new thread called:
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:
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:
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)
🧮 Line 2: int 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
✔ No Heap usage yet
✔ No Garbage Collector activity yet
📝 Line 4: Printing Value
What happens internally?
-
JVM creates String
"Sum: "→ stored in String Pool (Heap — Method Area-backed) -
JVM creates a new concatenated String →
"Sum: 30"→ Heap -
println()is a native method → uses Native Method Stack via JNI
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:
-
Stack frame for main is removed
-
Local variables (
x,y,sum) are gone -
Heap objects referenced by nothing → eligible for GC
-
PC Register for main thread resets
-
Main thread dies
-
JVM shuts down (if no other threads exist)
Full Summary with Memory Flow Diagram
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