🧠 What is Stack, Heap Memory, and String Pool in Java?

If you’ve started learning Java, you’ve probably heard people say things like:

“That variable is stored on the stack,” or “The object lives in the heap.”

And then there’s something called the String Pool — sounds confusing, right?
Don’t worry πŸ˜„ — this post will make everything super simple to understand.


🧩 Let’s start with: What happens when you run a Java program?

When your Java code runs, the JVM (Java Virtual Machine) divides memory into different sections.
The two most important ones are:

  1. Stack Memory

  2. Heap Memory

Let’s see what each one does πŸ‘‡


πŸ₯ž 1. Stack Memory — for short-term data

Think of Stack Memory as a temporary notebook πŸ“’ that the JVM uses to store small, short-lived data.

✳️ Used for:

  • Primitive variables (int, char, boolean, etc.)

  • References to objects (not the objects themselves)

  • Method calls and local variables

Every time a method is called, the JVM creates a stack frame for it.
When the method finishes, its frame is removed automatically.

πŸ”Ή Example:

void demo() {
    int a = 10;
    int b = 20;
}

🧠 Memory view:

Stack:
a → 10
b → 20

When demo() ends, both a and b are removed from the stack.

Key idea: Stack memory is fast but temporary.


🧱 2. Heap Memory — for long-term objects

Now imagine the Heap as a big warehouse πŸ—️ where Java stores objects created using the new keyword.

✳️ Used for:

  • Objects and arrays

  • Instance variables

  • String objects (created using new)

πŸ”Ή Example:

String name = new String("John");

🧠 Memory view:

Stack:
name → 0x101 (reference to heap)
Heap:
0x101 → "John"

Even if the method that created name ends, the object "John" stays in the heap until the Garbage Collector removes it (when no references point to it).

Key idea: Heap memory is shared and used for all objects in your program.


πŸ’¬ Stack vs Heap — quick comparison

Feature Stack Memory Heap Memory
Stores Primitives, references Objects, arrays
Lifetime Short (method-level) Long (until GC cleans up)
Managed by JVM automatically Garbage Collector
Access speed Very fast Slower
Thread safety Each thread has its own Shared by all threads

πŸ’Ž 3. String Pool — a special part of the Heap

Strings are special in Java because they are immutable (cannot be changed once created).
So Java uses a special memory area called the String Pool to reuse strings instead of creating duplicates.

πŸ”Ή Example:

String s1 = "Hello";
String s2 = "Hello";

🧠 Memory view:

Heap (String Pool):
"Hello" ←─ s1, s2

➡️ Both s1 and s2 point to the same string — no new object created.

But if you use new String("Hello"):

String s3 = new String("Hello");

Then a new object is created in the heap (outside the pool).

🧠 Memory view:

Heap:
String Pool: "Hello" ←─ s1, s2
Outside Pool: "Hello" ←─ s3

You can make it reuse the pooled version using .intern():

String s4 = new String("Hello").intern();
System.out.println(s1 == s4); // true

πŸš€ Summary

Memory Area Stores Lifetime Managed By Example
Stack Primitives, references Temporary (method-level) JVM int x = 10;
Heap Objects, arrays Long (until GC runs) Garbage Collector new Student()
String Pool String literals Long JVM "Hello"

🧠 Quick Recap:

  • πŸ₯ž Stack → short-term, stores primitive values and object references.

  • 🧱 Heap → long-term, stores actual objects and arrays.

  • πŸ’Ž String Pool → special memory inside the heap that reuses string literals to save space.


πŸ’¬ Final Thought

Understanding how Java memory works helps you write efficient and bug-free programs.
Next time you create an object or a string, you’ll know exactly where it lives in memory! πŸš€


Comments

Popular posts from this blog

Your build is currently configured to use incompatible Java 21.0.3 and Gradle 8.2.1. Cannot sync the project.

Error in Android Migration Gradle 7.5 to 8.5 - java.lang.NullPointerException: Cannot invoke "String.length()" because "" is null

Setting Up Jenkins for Flutter App on macOS