Stack vs Heap Memory in Java ☕

Stack vs Heap Memory in Java ☕

Welcome to the second article of our OOP series! In this one, we’re going to talk about something that many people skip but in my opinion, it’s super important to understand if you want to learn OOP properly: Stack Memory and Heap Memory.

💾 Where Does Your Program Live?

When we run a program on our computer, it gets loaded into the RAM. The Operating System (OS) is responsible for allocating memory to various things like application software, compilers, and system processes.

In our case, we’re running a Java program, so the OS will allocate memory for the Java Virtual Machine (JVM).


Article content

🛑 If you don’t know what JDK, JRE, and JVM mean, I recommend you check out my first blog post before going ahead. You can find the link at the bottom of this article.Okay, promotion done 😄 — now back to our topic!


🧠 Memory Inside the JVM

The memory given to the JVM by the OS is divided into different areas. The two main areas we’ll focus on are

Stack Memory

Heap Memory


📦 What is Stack Memory?

Just imagine a stack of boxes. When you create a primitive variable like int x = 10;, Java will create a small box in the stack and directly store 10 in it.

int x = 10;
double d = 10.5;
byte b = 8;        

All of these primitive values go into the Stack Memory. Each time you call a method, a new block (stack frame) is added to the stack. When the method ends, that block is removed this is why Stack is sometimes called the "method call stack".

➕ Stack is:

  • Fast
  • Limited in size
  • Stores primitive values and method call information


🧳 What is Heap Memory?

Now, let's talk about Heap Memory. This is a bigger and slower memory area compared to the stack. The Heap is where objects live.

int[] arr = {1, 2, 3, 4, 5};
arr[0] = 10;
        

Here’s what happens:

  • arr is a reference variable stored in the Stack.
  • The actual array {1,2,3,4,5} is stored in the Heap.
  • When you write arr[0] = 10;, you're updating the value inside the object stored in the Heap.


Article content

Here’s what’s really happening behind the scenes:

  • When we create the array, the array object (the list of values) is stored in the Heap, because arrays are objects in Java.
  • The Operating System provides an address (or memory location) in the heap for this array.
  • This address is stored in the stack, inside the variable arr.

So, the variable arr doesn’t actually hold the array itself. It just holds the reference (or pointer) to where the array lives in the heap.

That’s why we call arr a reference type variable or an address variable. it points to the real data in the heap.

The address is like a key to open the box in the heap where the actual object is stored. This system helps Java manage memory better, separating small, fast-access data (like addresses and primitive types) into the stack, and large objects into the heap.

⚖️ Why Stack and Heap Matter?

Well, if you're learning OOP but don’t know about Stack and Heap memory, you’re missing a big piece of the puzzle.

Here’s why it’s important:

  • Stack is small but fast — good for short-term values.
  • Heap is large and flexible — good for storing big and complex data like objects.
  • Managing memory correctly helps avoid errors like

🔁 Quick Recap (In Simple Words)

Stack memory stores small things like primitive variables (int, char, double, etc.) and method calls. It's fast and has limited space. Anything stored here will be removed once the method finishes running. The JVM (Java Virtual Machine) manages this memory automatically.

Heap memory is used to store bigger things like objects and arrays. It’s much larger than the stack but a bit slower to access. The data here stays until it's no longer needed, and then the Garbage Collector in the JVM removes it to free up space.

So in short:

  • Stack is for simple data and method calls. It’s fast and short-lived.
  • Heap is for objects and arrays. It’s bigger, slower, and lasts longer.
  • The JVM handles both, but uses different ways: stack is cleared automatically after use, while heap is cleaned by the Garbage Collector.

Understanding this will help you write better code and troubleshoot memory-related issues more easily.


👉 P.S. If you haven’t read my first blog post about JVM, JDK, and JRE — check it out here!

To view or add a comment, sign in

More articles by Yashen Fernando

  • Stop Using Double for Prices.

    When I started building my Daraz clone using Spring Boot, I spent a lot of time looking through professional GitHub…

    2 Comments
  • What is Data Science?

    Most people have no idea about Data Science. But we all know about physical science, biological science, and chemical…

    4 Comments
  • A Brief Idea About Object-Oriented Programming

    Object-Oriented Programming (OOP) is a popular programming paradigm that helps developers structure and organize their…

Others also viewed

Explore content categories