Kanhaiya.

🧠 Java Memory Management Architecture

public class Student {
    private String name;        // Instance variable
    private int age;           // Instance variable
    private static int count = 0;  // Static variable

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        count++;
    }

    public void displayInfo() {
        String message = "Student Info";  // Local variable
        int tempAge = this.age;          // Local variable
        System.out.println(message + ": " + name + ", " + tempAge);
    }
}

public class Main {
    public static void main(String[] args) {
        int num = 42;                    // Local primitive
        Student student1 = new Student("Alice", 20);  // Reference variable
        Student student2 = new Student("Bob", 22);    // Reference variable
    }
}

🥞 STACK MEMORY

main() method frame:
• int num = 42
• Student student1 = 0x1001 ➡️
• Student student2 = 0x1002 ➡️
displayInfo() method frame:
• String message = 0x2001 ➡️
• int tempAge = 20
• this = 0x1001 ➡️
Constructor frame:
• String name = 0x3001 ➡️
• int age = 20
• this = 0x1001 ➡️
Stack stores:
• Method call frames
• Local variables
• Method parameters
• Reference variables (addresses)
• Primitive values

🏔️ HEAP MEMORY

Student Object #1 (0x1001):
• name = 0x3001 ➡️ "Alice"
• age = 20
• Class metadata pointer ➡️
Student Object #2 (0x1002):
• name = 0x3002 ➡️ "Bob"
• age = 22
• Class metadata pointer ➡️
String Objects:
• 0x2001: "Student Info"
• 0x3001: "Alice"
• 0x3002: "Bob"
Heap stores:
• All objects
• Instance variables
• Arrays
• String literals
• Object metadata

📚 METHOD AREA

Student.class:
• Class metadata
• Method bytecode
• static int count = 2
• Constant pool
String.class, Object.class:
• System class metadata
• Method implementations
Method Area stores:
• Class-level metadata
• Static variables
• Method bytecode
• Constant pool
Primitive Variables
Reference Variables
Objects
Class Metadata

🔍 Key Memory Management Concepts:

Stack Memory: Each thread has its own stack. When a method is called, a new frame is pushed onto the stack containing local variables, method parameters, and return addresses. Primitive variables store actual values, while reference variables store memory addresses pointing to objects in the heap.

Heap Memory: Shared among all threads, the heap stores all objects and their instance variables. Objects are allocated here during runtime using the 'new' keyword. The heap is divided into Young Generation (Eden, Survivor spaces) and Old Generation for garbage collection optimization.

Method Area: Stores class-level data including class metadata, method bytecode, static variables, and the constant pool. This area is shared among all threads and loaded when classes are first referenced.

Memory Flow: When you create an object, the reference variable goes on the stack, but the actual object is allocated in the heap. The reference variable contains the memory address where the object is stored in the heap.