The three classic memory crashes
Now that you know the layout, the famous crashes make sense:
- Stack overflow: the stack has a fixed, smallish size (a few megabytes). If function calls keep nesting without returning, frames pile up until the stack runs out of room. The usual cause is runaway recursion.
- Out of memory (OOM): the heap can grow, but not forever. If a process keeps allocating, the OS eventually refuses (or a system OOM-killer terminates the process).
- Segmentation fault (segfault): the process touches an address it does not own, like following a bad pointer in C. The hardware traps it, the OS kills the process. This is process isolation (lesson 3-1) doing its job.
Python protects you from segfaults and converts stack overflow into a catchable RecursionError, but the machine underneath is the same.
Code exercise · python
Run this. Healthy recursion first: each call pushes a frame, each return pops one, and even 500 levels deep is fine.
Code exercise · python
Run this. forever() has no base case, so frames pile up until Python's stack limit stops it. We catch the RecursionError so you can see the crash without the crash.
Code exercise · python
Your turn. This countdown has NO base case, so it would overflow the stack. Add the base case (when n reaches 0, return "liftoff") so the recursion stops and the program prints liftoff.
Reading the crash like an engineer
When you see these in the wild:
| Symptom | Likely cause | First thing to check |
|---|---|---|
RecursionError / stack overflow | recursion with no (reachable) base case | does the recursive call always move toward the base case? |
Process killed, MemoryError, machine crawls then a process dies | unbounded growth: a list or cache that only ever grows | what collection grows on every request or loop iteration? |
Segmentation fault (core dumped) | native code touched memory it does not own | which C extension or native library was involved? |
The pattern to internalize: crashes are the OS and hardware enforcing the rules you learned in unit 1 and lesson 3-1, not random bad luck.
Quiz
A web service slowly uses more and more RAM over days, then the OS kills it. Restarting "fixes" it for a while. Which crash family is this?
Problem
A function calls itself but its base case can never be reached. Frames pile up until the program dies. What is this crash called? (two words)