Memory Management: Stack vs Heap
When your Java program runs, it needs memory. If you write int age = 21;, the computer needs somewhere to keep the value 21. If you create an object new Student();, the object also needs memory.
Java's runtime uses different memory areas for different purposes. Two of the most important concepts for beginners are: Stack and Heap.
A Simple House Analogy
Imagine a house with two areas:
- Stack = Your Work Desk: The desk contains things you are currently using while performing a task. When you finish the task, the things associated with it can be removed.
- Heap = Storage Room: The storage room contains objects that may need to exist independently of one particular task. They stay there until they are no longer needed.
The Stack
The stack is associated with method execution. When a method is called, the JVM creates a "stack frame" for that method. The frame contains information needed for that method's execution, including local variables and references.
Consider:
public static void main(String[] args) { int age = 21;}When main() finishes, its stack frame is removed. This is why local variables naturally disappear when a method is done.
The Heap
The heap is the memory area used for dynamically created objects and arrays. For example:
Student student = new Student();The expression new Student() creates a Student object inside the Heap.
Local Primitive vs Object Reference
Consider:
public static void main(String[] args) { int age = 21; Student student = new Student();}A useful simplified picture is:
ageis a local primitive variable. It lives entirely on the Stack.studentis a local reference variable. The reference lives on the Stack, but the actual object it points to lives on the Heap!
Visualizing Stack vs Heap: Interactive Memory Stepper 🕹️
Step through the live code below to see how Java manages primitives on the Stack, allocates objects in the Heap with memory addresses (0x4A), shares references, and triggers Garbage Collection:
Primitive data types (int, double, boolean) live directly inside the active method's Stack Frame. The raw value (21) is stored directly in the variable's memory slot.
No objects created with 'new' yet.
What Is Garbage Collection?
Java automatically manages the memory occupied by objects that are no longer reachable. This process is called Garbage Collection (GC).
Instead of manually telling Java: "Delete this object now", the JVM can identify objects on the Heap that no longer have any variables (references) pointing to them, and automatically clean them up!
Stack Overflow vs Heap Problems
- Stack Overflow: Deep or infinite recursion can consume stack frames. Eventually, the available stack space can be exhausted, resulting in a
StackOverflowError. - Heap Exhaustion: Creating too many objects without enough available heap memory can lead to an
OutOfMemoryError.
The mental model to remember is: Local variables and method execution conceptually live on the stack. Objects and arrays conceptually live on the heap.