How JVM Identifies and Communicates Between Stack, Heap, and Method Area
Java has three types of memory involved during execution:
1️⃣ Method Area
Stores class-level metadata
(Static variables, class names, method definitions, constant pool)
2️⃣ Java Stack
Stores method frames and local variables
(Primitives + references)
3️⃣ Heap
Stores objects, arrays, Strings
The key question:
👉 How does the JVM connect these separate memory areas?
Let’s break it down step-by-step.
1. The Key to Everything: References
Java does not store objects inside the Stack.
The Stack only stores a reference (like a pointer) pointing to the object in the Heap.
Example:
JVM Memory View:
✔ p does not contain the object itself
✔ It contains an identifier (reference address)
✔ JVM uses that reference to locate the object inside the Heap
2. What is a “Reference Address”?
It is not the real OS memory address.
Java hides OS addresses for safety.
Instead, the JVM uses:
-
Object handles
-
Internal pointers
-
Indirection tables
-
Metadata tables
Depending on HotSpot implementation, objects are tracked either by:
or
Either way:
A reference uniquely identifies and locates the object in the Heap.
3. How Stack Talks to Heap (Using References)
Example:
Java Stack:
Heap:
Flow
-
JVM looks inside Stack
-
Reads
p -
Finds reference
#A13F22 -
Goes into Heap to fetch object
-
Finds field
name -
Accesses string
"iPhone"
This is how the Stack communicates with the Heap.
4. How Heap Communicates with Method Area
When creating an object:
JVM first checks Method Area:
If NOT:
-
Class Loader loads Product.class
-
Method Area stores:
-
Field names
-
Method names
-
Method bytecode
-
Constant pool
-
Static variables
-
Only after this:
JVM creates an object → places it in Heap.
So each object in heap points back to its class data in Method Area.
This is how the Heap communicates with Method Area.
5. What Identifiers Does the JVM Use?
There are three major identifiers:
1️⃣ Local Variable Identifier (Stack)
Used for:
-
Primitive values (
int x) -
References to heap (
Product p)
Stack variables are identified by:
-
Name (
x,p) -
Slot number inside the frame
2️⃣ Object Reference Identifier (Heap)
Each object gets a unique reference (like an internal pointer):
This internal ID is what connects Stack → Heap.
3️⃣ Class Identifier (Method Area)
For each class:
-
Fully Qualified Name:
com.example.Product -
ClassLoader identity
-
Version information
Objects in Heap store a link to their class in Method Area.
6. End-to-End Communication Diagram
This is the full path:
7. What About Static Variables?
Static variables belong in Method Area.
Accessing them:
Example:
Heap is not involved at all.
8. What About Methods?
When you call:
Flow is:
-
Stack finds reference
p -
JVM goes to Heap object
-
Heap object points to Method Area
-
JVM finds method bytecode
-
Execution Engine runs the method
9. What About Thread Communication?
Each thread has:
-
Its own Java Stack
-
Its own PC Register
-
Its own Native Method Stack
But all threads share:
-
Method Area
-
Heap
This is why threads can share data (Heap)
but not each other’s local variables (Stack).
Final Simple Summary
✔ Stack
Stores names (variable identifiers)
Stores references (IDs pointing into heap)
✔ Heap
Stores objects
Each object has an internal ID and a link to its class in Method Area
✔ Method Area
Stores class definitions and static values
✔ Communication
Stack → Heap via reference ID
Heap → Method Area via class pointer
Everything begins with references.
One-Line Explanation
The Stack stores references, the Heap stores objects, and the Method Area stores class definitions. JVM connects all three using internal identifiers and pointers.
No comments:
Post a Comment