Java Memory Model Explained with a Simple Example

 

Java Memory Model Explained with a Simple Example

Understanding where variables and objects are stored in Java is a common interview topic and an important foundation for writing efficient and safe code.

Java divides memory at runtime into several areas:

  • Method Area (Metaspace)

  • Heap

  • Stack

  • String Constant Pool

Let’s understand all of them using one simple class and object.


✅ Example Program with JVM Memory Comments

/** * JVM RUNTIME DATA AREAS * * 1. Method Area (Metaspace) * - Class metadata * - Static variables * - Runtime constant pool * * 2. Heap * - Objects * - Instance (object) variables * * 3. Stack * - Method call frames * - Local variables * - Reference variables * * 4. String Constant Pool * - String literals (special area inside Method Area conceptually) */ class Employee { // ================================ // METHOD AREA (METASPACE) // ================================ // Static variable // One copy per class // Shared across all objects static String company = "Apple"; // "Apple" → String Constant Pool // ================================ // HEAP (Inside Employee Object) // ================================ // Instance variables (per object) int id; // primitive value stored inside heap object String name; // reference stored in heap object // actual String object stored in String Pool void work() { // ================================ // STACK (work() stack frame) // ================================ // Local variable (primitive) int hours = 8; // value stored directly in stack System.out.println(name + " works " + hours + " hours"); } } public class Main { public static void main(String[] args) { // ================================ // STACK (main() stack frame) // ================================ // Reference variable stored in stack // It points to an object in the heap Employee e = new Employee(); // ================================ // HEAP (Employee object) // ================================ // Instance variable inside heap object e.id = 101; // Reference stored in heap object // String literal "Vinod" stored in String Constant Pool e.name = "Vinod"; // Method call → new stack frame created e.work(); } }

๐Ÿง  How the Memory Looks at Runtime (Conceptual View)

STACK ----- main(): e ─────────────▶ Heap.EmployeeObject work(): hours = 8 HEAP ---- EmployeeObject id = 101 name ─────────▶ "Vinod" METHOD AREA (METASPACE) ---------------------- Employee.class static company ─▶ "Apple" STRING CONSTANT POOL ------------------- "Apple" "Vinod"

๐Ÿงช Variable Type vs Memory Location

Variable TypeExampleStored In
Local variableint hoursStack
Reference variableEmployee eStack
Instance variableid, nameHeap
Static variablecompanyMethod Area
String literal"Vinod"String Pool

๐ŸŽฏ Interview-Ready Explanation

Local variables and references are stored on the stack, objects and instance variables are stored in the heap, static variables are stored in the Method Area, and string literals are stored in the String Constant Pool.


๐Ÿ“ One-Line Takeaway

Stack stores method data, Heap stores objects, Method Area stores class data, and String Pool stores string literals.

No comments:

Post a Comment

Java Design Patterns With What • Why • When • Full Java Programs • Client Usage

  Java Design Patterns  With What • Why • When • Full Java Programs • Client Usage Introduction Design Patterns are proven solutions to...

Featured Posts