Understanding JVM Memory: From Method Area to Native Method Stack (Beginner to Advanced)

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:

  • Method Area
  • Heap Memory
  • Stack Memory
  • PC Register
  • Native Method Stack

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?

  • Class structure (Employee)
  • Method definitions (workHours())
  • Static variables (companyName)
  • Field definitions (name, age)

👉 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?

  • A new object is created in heap memory.

Example:

Heap Memory
-----------
Address 101
name: null
age: 0        

👉 Objects always live in the heap.


📌 3. Stack Memory (Method Execution + References)

Stack memory stores:

  • Method calls (stack frames)
  • Local variables
  • Reference variables (not objects!)

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:

  • Objects → Heap
  • References + local variables → Stack
  • Static variables → Method Area


⚡ 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:

  • Current line being executed
  • Next instruction to execute

Why is this important?

  • Enables multi-threading
  • Each thread runs independently
  • JVM knows exactly where each thread is executing


🔌 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?

  • This method is implemented outside Java (in native code)
  • JVM uses Native Method Stack to handle it

What it stores:

  • Local variables of native methods
  • Execution context of native code

👉 It works similarly to the Java stack, but for non-Java code


🚀 Key Takeaways

  • Java uses separate memory areas for efficiency
  • Objects are always in the heap
  • References and method calls are in the stack
  • Static data belongs to the method area
  • Each thread has its own PC register
  • Native code runs using the native method stack


🧠 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)


To view or add a comment, sign in

More articles by Heshanth Zimmendra

Others also viewed

Explore content categories