The memoryOverhead OOM
Everything so far lives inside the JVM heap. The cluster manager does not police the heap, though. It limits the whole process, and it sets that limit at the heap plus spark.executor.memoryOverhead. When the process's resident memory crosses the limit, the cluster manager kills it outright, with no exception, no spill and no chance to finish the task.
- 384 MB
- 10%
- 137
What lives outside the heap
- Thread stacks for every task, shuffle and network thread the executor runs
- The JVM's own metadata, such as loaded classes and compiled code
- Direct byte buffers the network layer uses to send and fetch shuffle blocks
- Native memory used by compression libraries and other code outside the JVM
- Python worker processes the executor starts to run your Python functions
None of this appears in Spark's memory accounting, so none of it can spill or be evicted. The default overhead is the larger of the floor and the share: for the running example's 8,192 MB heap, 10% gives 819 MB, and the cluster manager grants a container of 9,011 MB.
Heap error or container kill
- Heap OutOfMemoryError
- The JVM throws java.lang.OutOfMemoryError: Java heap space inside a task
- The executor log holds the error and its stack trace
- The task fails with the error as its reason
- Fix with less data per task, more heap, or fewer cores
- Container kill
- The cluster manager kills the process from outside the JVM
- The executor log stops mid-stream, with no OutOfMemoryError
- The driver reports ExecutorLostFailure, often with exit code 137, which is 128 plus signal 9
- Fix with more spark.executor.memoryOverhead or fewer concurrent tasks
The tell is the mismatch. In the Executors tab, covered in the next section, a dead executor's storage memory can read far below its total, and its last log lines can be routine task progress, because the heap never ran out. The process outgrew its limit in memory the JVM does not count. The driver's failure reason starts with ExecutorLostFailure (executor 7 exited caused by one of the running tasks), followed by the cluster manager's own words: the container was killed for exceeding memory limits.
Raising the overhead
Fix the region that grew. Raising spark.executor.memory from 8g to 9g grows a heap that had room and adds only about 100 MB of overhead through the 10% share. Set the overhead directly instead:
from pyspark.sql import SparkSession
spark = (
SparkSession.builder
.appName("trips_nightly")
.config("spark.executor.memory", "8g")
.config("spark.executor.cores", "4")
.config("spark.executor.memoryOverhead", "2g")
.getOrCreate()
)The container grows from 9,011 MB to 10,240 MB, and every added megabyte lands outside the heap, where the kills came from. Spark 3.3 added spark.executor.memoryOverheadFactor for setting the share instead of a size: raising it from 0.10 to 0.20 gives the same 8 GB heap about 1,638 MB of overhead.
Executors keep dying mid-stage. The driver shows ExecutorLostFailure with exit code 137, the executor logs end with no OutOfMemoryError, and the dead executors' storage memory read 0.9 GiB of 4.4 GiB. Each has an 8g heap and the default overhead. What do you change?
- Raise spark.executor.memory to 9g and keep the default overhead.
- Raise spark.executor.memoryOverhead to 2g and keep the 8g heap.
- Lower spark.memory.fraction to 0.4 so that user memory grows.
- Call unpersist() on the cached DataFrame before the stage runs.
About this lesson
This section is part of the Memory and Caching: Intermediate 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.