Course outline · 0% complete

0/28 lessons0%

Course overview →

Why programs crash

lesson 4-2 · ~11 min · 12/28

The three classic memory crashes

Now that you know the layout, the famous crashes make sense:

  1. 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.
  2. 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).
  3. 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:

SymptomLikely causeFirst thing to check
RecursionError / stack overflowrecursion with no (reachable) base casedoes the recursive call always move toward the base case?
Process killed, MemoryError, machine crawls then a process diesunbounded growth: a list or cache that only ever growswhat collection grows on every request or loop iteration?
Segmentation fault (core dumped)native code touched memory it does not ownwhich 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)