Memory Management

Memory questions arrive as symptoms: a stage that got slow, an executor that keeps dying, an error message pasted into the chat. The skill under test is matching each symptom to the region of memory that ran out, and that takes a picture of how an executor divides the memory it was given.

How a 16 GB executor divides its memory

RegionSize on a 16 GB executorWhat it holds
Reserved300 MBFixed memory for the engine's own internals
ExecutionShares 9.4 GB with storageShuffle buffers, plus the sort and hash structures behind joins and aggregates
StorageShares 9.4 GB with executionCached DataFrames and broadcast tables
User6.3 GBObjects your own code creates, such as data structures inside a UDF
Overhead, outside the heap1.6 GBNetwork buffers, native memory and Python worker processes

The split comes from 2 settings. spark.memory.fraction, 0.6 by default, gives 60% of the heap left after the reserved 300 MB to a unified region, roughly 9.4 GB here, which execution and storage share. Execution can evict cached blocks to grow, down to the half that spark.memory.storageFraction protects, while cached blocks can never evict running execution memory. Inside execution, each running task may take at most an equal share, so with 4 tasks busy on 4 cores, each task gets roughly 2.4 GB before it has to spill.

Read the symptom, then the region

SymptomRegion that ran outUsual causeFirst fix
Stage slows; stage metrics show spill (memory) and spill (disk)Execution share per taskShuffle partitions larger than the 2.4 GB per-task shareRaise `spark.sql.shuffle.partitions`
Executor lost with `OutOfMemoryError: Java heap space`Executor heapA skewed key, or a broadcast too large for every executorFind the hot key; drop the broadcast
Container killed by the cluster manager for exceeding memory limitsOverhead outside the heapPython UDF workers or native buffersRaise `spark.executor.memoryOverhead`, leaving the heap as it is
Driver fails with `OutOfMemoryError`Driver heap`collect()` on a large result, or building a big broadcastWrite results to storage instead
Long pauses and high GC timeExecutor heapMillions of short-lived objects, or too much cached dataCache less; prefer built-in functions over UDFs

Spill is the slow but safe outcome. When the hash table a task builds for the aggregate reaches its 2.4 GB share, the task sorts what it holds and writes it to local disk. With that memory freed it keeps reading, and at the end it merges the spilled runs with what remains in memory. With 200 shuffle partitions, every reduce task in the nightly job receives 3.2 GB, so every one of them spills, and each spilled byte is written and read back at least once more. The stage metrics report spill (memory), the deserialized size of what was evicted, and spill (disk), its compressed size on disk, usually far smaller.

More partitions is the fix, and the target size follows from the share. Divide the shuffle bytes by a target of a few hundred megabytes and set spark.sql.shuffle.partitions to the result. Deserialized rows plus hash table structures can take several times their serialized size, so a partition well under the share leaves the headroom it needs.

Skew: when more partitions stop helping

More partitions divide the keys among more tasks, yet every row for a given key still lands in a single task. Suppose the job also joins events to a large profiles table on profile_id, and a test profile driven by an automated player logs 30 GB of play events in a day. At 200 partitions or 1,000, the task that receives that profile_id gets all 30 GB, spills for its whole run or dies with a heap error, while the other 999 tasks finish in seconds. The stage view gives it away: the maximum task input sits far above the median. If the key is junk, filter it out before the shuffle. If it is real, add a random suffix from 0 to 9 to that key on the events side, copy the matching profile row 10 times with each suffix, and join on the salted key. Adaptive execution can split oversized join partitions on its own; salting is the manual version of the same idea.

Fix the region that ran out

Do
  • Read spill and the largest task's input in the stage view before changing any setting
  • Size shuffle partitions from shuffle bytes divided by a target of a few hundred MB
  • Raise memory overhead when the cluster manager kills containers for exceeding limits
  • Compare the largest task to the median when 1 executor keeps dying
Avoid
  • Double executor memory as a first move; it hides skew and doubles the bill
  • Call collect() on a result whose size you have not checked
  • Cache a DataFrame that is read only once; it takes memory execution could use
  • Broadcast a table without checking its size once loaded as rows

About this lesson

This section is part of the Distributed Compute: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.