Understanding JVM Memory: From Method Area to Native Method Stack (Beginner to Advanced)
If you're starting with Java, one of the most important concepts to understand is how memory works inside the JVM (Java Virtual Machine). This directly affects performance, debugging, and even how you design applications.
Let’s break this down step by step using your example.
🧠 JVM Memory Structure Overview
The JVM divides memory into several key areas:
Each one has a very specific role.
🏗️ 1. Method Area (Class-Level Data)
This is where class definitions (blueprints) are stored.
When you write:
public class Employee {
String name;
int age;
static String companyName = "ClockworksAcademy";
public void workHours() {
int hours = 8;
System.out.println(name + " works " + hours + " hours a day at " + companyName);
}
}
What goes into Method Area?
👉 Think of this as a template storage — not actual data.
📦 2. Heap Memory (Objects Live Here)
Heap memory is where actual objects are stored.
Employee emp1 = new Employee();
What happens?
Example:
Heap Memory
-----------
Address 101
name: null
age: 0
👉 Objects always live in the heap.
📌 3. Stack Memory (Method Execution + References)
Stack memory stores:
Example:
Employee emp1 = new Employee();
Stack:
emp1 → 101
👉 emp1 does NOT store the object — it stores the reference (address).
🔄 Full Example Walkthrough
Employee emp1 = new Employee();
emp1.age = 25;
emp1.name = "Heshanth";
Employee emp2 = new Employee();
emp2.age = 30;
emp2.name = "Rahul";
emp2.workHours();
Heap Memory
Address 101
name: "Heshanth"
age: 25
Address 102
name: "Rahul"
age: 30
Stack Memory
Main Method Frame:
emp1 → 101
emp2 → 102
workHours() Frame:
hours → 8
👉 Important:
Recommended by LinkedIn
⚡ 4. PC Register (Program Counter)
Now to your question:
What is the PC Register?
Each thread has its own PC Register.
Thread 1 → PC Register 1
Thread 2 → PC Register 2
Thread 3 → PC Register 3
Its job:
👉 It stores the address of the current instruction being executed
Think of it like:
“Where am I in the code right now?”
Example:
If your code is:
emp2.workHours();
Inside workHours(), the PC register keeps track of:
Why is this important?
🔌 5. Native Method Stack
Java is not always pure Java.
Sometimes, it calls methods written in other languages like C or C++.
Example:
public native int hashCode();
What happens here?
What it stores:
👉 It works similarly to the Java stack, but for non-Java code
🚀 Key Takeaways
🧠 Full JVM Memory Diagram
┌──────────────────────────────┐
│ METHOD AREA │
│------------------------------│
│ Class: Employee │
│ - name │
│ - age │
│ - workHours() │
│ - static companyName │
│ │
│ Class: Main │
│ - main() │
└──────────────────────────────┘
┌──────────────────────────────┐
│ HEAP │
│------------------------------│
│ Object @101 (emp1) │
│ name: "Heshanth" │
│ age: 25 │
│ │
│ Object @102 (emp2) │
│ name: "Rahul" │
│ age: 30 │
└──────────────────────────────┘
┌──────────────────────────────┐
│ THREAD 1 (main) │
│------------------------------│
│ PC REGISTER │
│ → current instruction │
│------------------------------│
│ STACK │
│------------------------------│
│ Frame: workHours() │
│ hours = 8 │
│------------------------------│
│ Frame: main() │
│ emp1 → 101 │
│ emp2 → 102 │
│------------------------------│
│ NATIVE METHOD STACK │
│ (used if native called) │
└──────────────────────────────┘
┌──────────────────────────────┐
│ THREAD 2 │
│------------------------------│
│ PC REGISTER │
│------------------------------│
│ STACK │
│------------------------------│
│ (its own method calls) │
│------------------------------│
│ NATIVE METHOD STACK │
└──────────────────────────────┘
⚡ Power Note — JVM Stack vs Heap
✔️ When a method finishes, its stack frame is removed automatically ❗ Objects in the heap are not immediately removed — they are cleaned up later by the Garbage Collector
📚 Source (Video)