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).
🛑 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:
🧳 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.
Recommended by LinkedIn
int[] arr = {1, 2, 3, 4, 5};
arr[0] = 10;
Here’s what happens:
Here’s what’s really happening behind the scenes:
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:
🔁 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:
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!
Love this, Yashen