The Plain Picture#
Two ways to keep papers on your desk:
A spike — you push each new sheet down onto a metal spike. You can only take the top sheet off. It's super fast and tidy, and when you finish a task, you pull its sheets off in one go. But the spike is short.
A big table — you can put papers anywhere, of any size, keep them as long as you like. But you have to remember where you put things, and someone must clear away papers nobody needs anymore.
The Real Names#
The spike is the stack: memory for function calls. Each call pushes a stack frame holding the function's local variables and where to return to. When the function returns, its frame is popped off instantly.
The big table is the heap: memory for data that must outlive a single function call, or whose size isn't known in advance — objects, lists, strings.
Clearing the table: in C you free heap memory by hand. In Java, Python and JavaScript, a garbage collector (GC) finds heap data that nothing refers to anymore and frees it automatically.
(Different meaning warning: "heap" is also the name of a data structure — the priority queue in DSA. The two are unrelated; it's just a reused word.)
Under the Hood#
A Java example (Java details come in Classes, Objects and Memory):
static int bonus(int score) {
int extra = 50;
return score + extra;
}
public static void main(String[] args) {
int score = 200;
String note = new String("Hi");
int total = bonus(score);
}While bonus is running, memory looks like this:
STACK HEAP ┌──────────────────────────┐ │ frame: bonus │ ← top (pushed last) │ score = 200 │ │ extra = 50 │ │ return to: main │ ├──────────────────────────┤ │ frame: main │ │ score = 200 │ ┌──────────────────┐ │ note = ref ───────────┼──────────────▶ │ String "Hi" │ │ total = (not yet set) │ └──────────────────┘ └──────────────────────────┘
Notice: score and extra (simple numbers) live right inside the stack frames. The String object
lives on the heap, and the stack only holds a reference to it — an address, exactly as in
How Computers Work Ch 7. When bonus returns, its frame disappears; when main ends and nothing
refers to the "Hi" object, the garbage collector can reclaim it.
Stack vs heap at a glance:
| Stack | Heap | |
|---|---|---|
| Holds | local variables, call frames | objects, data that outlives a call |
| Size | small (typically ~1–8 MB per thread) | large (up to available RAM) |
| Speed | very fast (push/pop) | slower (allocation + garbage collection) |
| Freed | automatically when the function returns | by the garbage collector (or by hand in C) |
| Shared between threads? | no — each thread has its own (Ch 7) | yes — all threads of a process share it |
Stack overflow#
Every recursive call (a function that calls itself) pushes another frame. If recursion goes too deep,
the stack runs out: stack overflow. In Java this is a StackOverflowError; in Python it's
RecursionError (Python stops you at a depth of 1,000 by default).
This is a real DSA concern: a depth-first search on a graph with 100,000 nodes in a straight line can overflow the stack if written recursively. The fix is to use your own stack data structure on the heap instead (you'll do this in DSA).
Gotchas
Garbage collection pauses. While the GC works, the program can pause — usually for milliseconds. In low-latency services, big GC pauses show up as occasional slow requests.
Memory leaks still happen with a GC. If you keep a reference to data you'll never use again (say, a global list that only grows), the GC can't free it. This is the most common "memory keeps growing until OOMKilled" bug (Ch 5).
Check yourself
4 questionsWhat goes on the stack, and what goes on the heap?
Show answerHide answer
Stack: function call frames with local variables and return addresses. Heap: objects and data that outlive a single call.
What happens to a function's local variables when it returns?
Show answerHide answer
Its stack frame is popped and they're gone.
What causes a stack overflow?
Show answerHide answer
Too many nested calls (usually deep or endless recursion), using up the fixed-size stack.
Can you get a memory leak in a garbage-collected language? How?
Show answerHide answer
Yes — by keeping references to data you no longer need, so the GC can't free it.