Memory & partition tuning

Executor memory layout, spill to disk, partition sizing (aim ~100-200MB per task), repartition vs coalesce, caching honestly. "Why did it OOM?" has about five answers — know them.

About 26 min read3-question knowledge check

The mental model

An executor's memory is a kitchen with a fixed counter, and every dish either fits on the counter or gets shoved onto the floor to finish later. Spark doesn't fail when a task needs more memory than it has — usually it spills: it writes the overflow to local disk and keeps going, slowly. So the common experience isn't a crash, it's a job that mysteriously takes 4× longer because half its work is happening on the floor (disk) instead of the counter (RAM). Memory tuning is mostly about keeping the working set on the counter — and knowing which of the five different things labelled "out of memory" actually happened.

The analogy that sticks: a shared kitchen with rooms. The whole building is the **executor JVM** (spark.executor.memory). A chunk is walled off as **reserved** for the building's own systems. The rest is a **unified region** split between two roommates who share fluidly: **execution memory** (the counter where shuffles, joins, sorts, and aggregations do their work) and **storage memory** (the pantry where cached DataFrames live). They borrow from each other — if nobody's caching, execution can use the whole region; if execution needs space, it can evict cached blocks. Outside the JVM entirely is **overhead memory** (spark.executor.memoryOverhead) for off-heap buffers, Python worker processes, and network — the part people forget until YARN/Kubernetes kills the container for exceeding its total limit.

The one idea that resolves most confusion: **"it OOMed" has about five distinct causes, and the fix is different for each.** A task spilling to disk, a partition too big to fit one task, a collect() overwhelming the driver, the container exceeding its total memory limit (overhead), or a genuine per-task allocation blowout — these look similar in a ticket and demand opposite fixes (more partitions vs more memory vs less driver materialisation vs more overhead). Diagnosing *which* is the whole skill, and partition sizing is the lever that fixes most of them, because the size of one partition is the size of one task's working set.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision