Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Your desk = Stack. Things you're currently working on, temporary, limited space, cleared when done. Warehouse = Heap. Large long-term storage, things stay until no longer needed.
Two Memory Areas
javascript
QUBITS OF DPK
Memory Visualization
java
QUBITS OF DPK
javascript
QUBITS OF DPK
Stack Frame Lifecycle
javascript
QUBITS OF DPK
Garbage Collection
java
QUBITS OF DPK
GC runs automatically. You can suggest System.gc() but JVM may ignore it.
String Pool
java
QUBITS OF DPK
Stack is Per Thread, Heap is Shared
javascript
QUBITS OF DPK
Comparison Table
Real Production Story
When Deepak's notification microservice crashed in production — 1 lakh alerts loaded into heap at once — OutOfMemoryError. Quick fix: increase heap from 512MB to 2GB. Real fix: paginate the query so only chunks load at a time. Heap usage stays constant regardless of alert volume. This is senior engineer thinking.
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"JVM has two main memory areas. Stack is fast, small, per-thread, and manages method frames via LIFO — a frame is pushed on call and popped on return. It stores local variables, primitive values, and object references. Heap is large, shared across threads, managed by GC, and stores all objects and arrays. Reference variable lives in stack but actual object is in heap. String literals go to String Pool in heap — identical literals share same object for efficiency. StackOverflowError on stack exhaustion, OutOfMemoryError on heap exhaustion."
Interview Questions & MAANG-Level Answers
Q1. Where are objects stored — stack or heap?
Objects are stored in the Heap. The reference variable (pointer to the object) lives in the Stack. Car myCar = new Car() — myCar (4/8 bytes reference) is in stack, the Car object with all its fields is in heap. Heap is shared across all threads. Stack is private per thread. GC manages heap; stack is managed automatically via LIFO.
Q2. What is a stack frame?
A stack frame is created for every method invocation and contains: the method's local variables, parameters, return address, and operand stack for intermediate calculations. Frames are pushed on call and popped on return. Since each thread has its own stack, method calls in different threads are completely isolated. Stack size is typically 512KB-1MB — deep recursion exceeds this, causing StackOverflowError.
Q3. What is the String Pool and why does it exist?
String Pool is a special cache in Heap where String literals are stored and reused. When you write String a = "hello"; String b = "hello";, both point to the SAME "hello" object in the pool. This saves memory since strings are immutable and can be safely shared. new String("hello") bypasses the pool and creates a new heap object. String.intern() can manually add a string to the pool.
Q4. Why is heap shared between threads dangerous?
All threads share the same heap, so multiple threads can read and write the same objects simultaneously. Without synchronization, this causes race conditions — inconsistent data, lost updates. Example: two threads both doing count++ (read-increment-write are 3 separate steps) — one thread's increment can overwrite the other's. Solutions: synchronized blocks, volatile variables, AtomicInteger, or concurrent data structures like ConcurrentHashMap.
Q5. What causes StackOverflowError vs OutOfMemoryError?
StackOverflowError: call stack is full — caused by infinite/deep recursion where method calls keep adding frames until stack space is exhausted. Fix: add base case, reduce recursion depth, or increase stack size with -Xss. OutOfMemoryError: heap is full — caused by creating too many objects, memory leaks (objects referenced but not used), or loading massive data into memory. Fix: increase heap with -Xmx, fix memory leaks, use pagination/streaming for large data (as you did in your notification service!).