π§ 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:
- 
Stack Memory 
- 
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
Post a Comment