Stack and Heap Memory In Java

    Stack and Heap Memory In Java

    Confused about stack vs heap memory in Java? This beginner-friendly guide explains the key differences with examples, diagrams, and real-life code.

    default profile

    Munaf Badarpura

    July 12, 2025

    4 min read

    If you are learning Java, you might have come across terms like stack memory and heap memory and wondered, "What's the difference?" You're not alone! These two play a big role in how Java stores and manages data during program execution.

    In this blog, I will explain stack vs heap memory in a super simple way. For better understanding, no confusing terms. Just clear, beginner-friendly explanations with examples.

    What is Stack Memory?#

    Stack memory is where Java stores method calls and local variables. It's fast, organized like a stack of plates (last in, first out), and gets cleaned up automatically when a method finishes.

    Every time you call a method, a new block is added to the stack. When the method is done, its block is removed.

    Think of it like this:

    • You call a method: Java pushes it onto the stack.
    • The method finishes: Java pops it off the stack.

    What goes into Stack Memory?

    • Primitive data types (like int, char, boolean, etc.) declared inside methods.
    • References to objects in the heap.
    Stack Memory Diagram

    Example:#

    public class StackExample { public static void main(String[] args) { int a = 10; // stored in stack int b = 20; // stored in stack int sum = add(a, b); // method call goes to stack System.out.println("Sum: " + sum); } public static int add(int x, int y) { return x + y; // x and y are also in stack } }

    Here, all variables like a, b, sum, x, and y are stored in stack memory. And both method calls (main and add) sit on the stack during execution.

    What is Heap Memory?#

    Heap memory is where Java stores objects and their instance variables. Unlike the stack, it's shared across the whole application and not cleaned up automatically when a method finishes. Instead, Java uses a Garbage Collector to remove unused objects from the heap.

    Think of it like this:

    • You create an object, it lives in the heap.
    • You stop using it then the Garbage Collector removes it (eventually).

    What goes into Heap Memory?

    • Objects created using new
    • Instance variables (fields) of objects
    Heap Memory Diagram.png

    Example:#

    class Car { String model; // stored in heap } public class HeapExample { public static void main(String[] args) { Car myCar = new Car(); // Car object is in heap myCar.model = "Tesla"; // 'model' also in heap } }

    Here, the Car object and its field model are stored in heap memory. But the reference myCar is stored in stack memory, pointing to the object in the heap.

    Differences Between Stack Memory and Heap Space in Java#

    The Stack and Heap has different similarities both in size and efficiency. In this section i will outline the core differences between the stack and Heap space in java.

    1. In the memory management, Stack follows the LIFO order while the Heap does not follows any order, as it is a dynamic data structure and it’s space complexity tends to be dynamic, allowing for flexible allocation and deallocation of memory.
    2. The Stack is used for a single thread of execution while Heap is dynamic and robust is used by the entire application.
    3. Whenever an object is invoked or created, it’s stored automatically into the Heap memory, while the stack memory contains the local variables and reference to the new object created.
    Stack And Heap Memory Diagram

    Stack vs Heap - Quick Comparison#

    FeatureStack MemoryHeap Memory
    Used forMethod calls, local variablesObjects and their fields
    Memory sizeSmallLarge
    Access speedFasterSlower
    Auto-cleanupYes (when method ends)No (Garbage Collected)
    LocationThread-specific (each thread has its own)Shared across all threads

    Conclusion#

    in short, Stack = short-term, fast, and method-focused and Heap = long-term, object-focused, and managed by Java's Garbage Collector, I hope this article made stack and heap memory easy to understand.

    Java
    Stack Memory
    Heap Memory
    Stack Vs Heap
    Memory Management In Java

    More articles