Read this first
Most executor memory incidents come from raising the wrong number. A container killed by the resource manager is not fixed by more heap, and a Java heap OOM is not fixed by more overhead. Identify which memory region ran out, then change only that one.
Note
On Databricks you choose a node type and the platform derives executor memory. Don't set
spark.executor.memorythere unless support tells you to. The model below still explains the errors you see.
The container: what the resource manager actually allocates
container = spark.executor.memory (JVM heap, -Xmx)
+ spark.executor.memoryOverhead (native, JVM metaspace, threads, netty, Python unless set separately)
+ spark.memory.offHeap.size (only if spark.memory.offHeap.enabled=true)
+ spark.executor.pyspark.memory (only if set)
| Setting | Default | Notes |
|---|---|---|
spark.executor.memory | 1g | JVM heap |
spark.executor.memoryOverhead | max(384m, factor × executor.memory) | Non-heap memory the container is allowed |
spark.executor.memoryOverheadFactor | 0.10 | Spark 3.3+. On Kubernetes, non-JVM jobs (PySpark, R) default to 0.40. Replaces spark.kubernetes.memoryOverheadFactor |
spark.memory.offHeap.enabled / .size | false / 0 | Tungsten off-heap pages. Size is added to the container request |
spark.executor.pyspark.memory | unset | Caps Python worker memory. If unset, Python lives inside overhead |
spark.driver.memory / spark.driver.memoryOverhead | 1g / same formula | Driver equivalents |
Overhead names, factors and minimums have changed across 3.x and 4.x releases. Check spark.conf on your build rather than trusting a blog table, including this one.
for k in ["spark.executor.memory", "spark.executor.memoryOverhead",
"spark.executor.memoryOverheadFactor", "spark.executor.cores",
"spark.memory.fraction", "spark.memory.storageFraction",
"spark.memory.offHeap.enabled", "spark.memory.offHeap.size",
"spark.executor.pyspark.memory", "spark.sql.shuffle.partitions"]:
print(k, spark.conf.get(k, "<unset>"))
Inside the heap: unified memory
usable = heap - 300 MB reserved
unified = usable × spark.memory.fraction (default 0.6)
storage = unified × spark.memory.storageFraction (default 0.5, protected from eviction)
execution = the rest, and it can borrow unused storage
user memory = usable × (1 - spark.memory.fraction) (your objects, UDF data structures, metadata)
Execution memory holds shuffle buffers, sort buffers, hash tables for joins and aggregations. Storage holds cached data and broadcast variables. Execution can evict cached blocks above the protected storage region. Storage cannot evict execution memory.
Per-task arithmetic
With N tasks running on an executor (N = spark.executor.cores / spark.task.cpus), each task can get between 1/(2N) and 1/N of the execution pool.
| Executor | Cores | Unified pool | Max per task (~1/N) |
|---|---|---|---|
| 8 GB heap | 4 | (8192 − 300) × 0.6 ≈ 4.7 GB | ≈ 1.2 GB |
| 16 GB heap | 4 | ≈ 9.6 GB | ≈ 2.4 GB |
| 16 GB heap | 8 | ≈ 9.6 GB | ≈ 1.2 GB |
| 32 GB heap | 5 | ≈ 19 GB | ≈ 3.8 GB |
Doubling cores on the same heap halves the memory each task can use. When a task needs more than its share it spills to disk, which is slow but not fatal. It only fails if it cannot get even the minimum share, or if the data structure is in user memory, outside Spark's accounting.
Tip
To give each task more memory, fewer cores per executor often works better than more heap. Splitting into more partitions (smaller tasks) usually works better than either.
Reading the Spark UI
| Where | What it tells you |
|---|---|
| Stage page, Spill (Memory) / Spill (Disk) | Tasks exceeded their execution share. Disk spill is the on-disk serialized size |
| Stage page, Peak Execution Memory per task | How close tasks got to their share |
| Executors tab, Storage Memory "used / total" | Total is the unified pool, not the heap. Use it to check your arithmetic |
| Executors tab, GC Time | Above about 10% of task time means heap pressure |
| Executors tab, dead executors | Look at the reason for loss: container kill or OOM |
Error to fix map
| Message | Region that ran out | First fix |
|---|---|---|
Container killed by YARN for exceeding memory limits. X GB of Y GB physical memory used. Consider boosting spark.executor.memoryOverhead | Overhead (native, Python, netty, off-heap) | Raise spark.executor.memoryOverhead or spark.executor.memoryOverheadFactor |
Kubernetes pod OOMKilled, exit code 137 | Container total | Same as above. Check whether spark.executor.pyspark.memory is set |
java.lang.OutOfMemoryError: Java heap space on an executor | Heap, often user memory or one huge record or group | More partitions, fewer cores per executor, then more heap |
java.lang.OutOfMemoryError: GC overhead limit exceeded | Heap | As above. Check for large collect_list or wide rows |
SparkOutOfMemoryError: Unable to acquire N bytes of memory, got 0 | Execution pool | Fewer concurrent tasks, more partitions, check skew |
Python worker exited unexpectedly (crashed) with no Java OOM | Python process outside the JVM | Raise overhead or set spark.executor.pyspark.memory. Reduce spark.sql.execution.arrow.maxRecordsPerBatch for pandas UDFs |
Total size of serialized results ... is bigger than spark.driver.maxResultSize | Driver result buffer (default 1g) | Stop collecting large results. Write to storage instead. Raise only if truly needed |
Driver OutOfMemoryError during broadcast | Driver heap | See the broadcast join post: lower or disable the auto-broadcast threshold for that query |
Patterns that eat memory outside Spark's accounting
- pandas UDFs and
applyInPandas. Each group or Arrow batch is materialised in the Python worker. One huge group causes a container kill, not a spill. collect_list/collect_seton skewed keys. One task builds a giant array in heap.- Many small files. Large file listings and Parquet footers live on the driver.
- Native libraries (compression codecs, ML libraries, RocksDB state store). They use overhead, not heap. RocksDB state store memory is configured separately. See the RocksDB post.
- Too many executor threads, for example very high
spark.executor.coresplus large netty buffers.
Sizing starting points
spark-submit \
--conf spark.executor.memory=16g \
--conf spark.executor.cores=4 \
--conf spark.executor.memoryOverheadFactor=0.15 \
--conf spark.sql.shuffle.partitions=800 \
--conf spark.sql.adaptive.enabled=true \
--conf spark.sql.adaptive.coalescePartitions.enabled=true \
app.py
Set shuffle partitions high enough that the biggest stage gets tasks of roughly 100–200 MB each, and let AQE coalesce the small ones.
| Workload | Starting shape | Why |
|---|---|---|
| JVM-only SQL / DataFrame ETL | 4–5 cores, 4–8 GB heap per core, overhead factor 0.10 | Balanced per-task memory, manageable GC |
| PySpark with pandas UDFs | Same cores, overhead factor 0.25–0.40 or explicit pyspark.memory | Python workers live outside the heap |
| Heavy caching | Keep memory.fraction default, raise heap | Storage borrows from execution only when it is free |
| Huge joins / aggregations that spill | More partitions first, then fewer cores per executor | Raises the per-task share without bigger containers |
Warning
Don't raise
spark.memory.fractiontowards 1.0 to stop spills. User memory shrinks, and code that builds objects (UDFs, collect functions) then fails with heap OOM instead of spilling. Leave it at the default unless you have measured a reason to change it.
Prevent it next time
- Record each production job's executor shape (heap, cores, overhead, partitions) in the job definition, not in someone's notebook.
- Alert on executor loss and on stages where disk spill is larger than the input, which is an early sign the job is about to fail.
- When data volume grows, scale partitions first and executor size second, and re-check the per-task arithmetic.
- After a Spark or runtime upgrade, re-check the defaults above. Overhead factors and minimums have changed between releases.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get