OOM Forensics
An out-of-memory failure names a place, and the place picks the fix. Raising spark.executor.memory after a driver OOM changes nothing, and raising it after a container kill puts most of the new memory into a heap that never filled. Asked in this order, 3 questions classify almost every case.
Classifying the failure
- Which process failed? A driver OOM fails the whole application at once. An executor failure shows in the driver as a failed task, with the error as its failure reason, or as a lost executor.
- Did the JVM throw, or was the process killed from outside? A thrown OutOfMemoryError carries a Java stack trace. A kill leaves an exit code, usually 137 or 143, and no trace.
- What was running? The failed task's stage, its operator and the size of its partition show whether a single task was oversized or every task shared too little.
| Where | What you see | Usual cause | First fix |
|---|---|---|---|
| Driver heap | `OutOfMemoryError: Java heap space` on the driver, ending the application | A large `collect()`, or building a broadcast table | Keep results on the cluster: aggregate first or write them out |
| Driver result limit | `Total size of serialized results ... is bigger than spark.driver.maxResultSize` | The same large `collect()`, stopped at the 1g default | The same; raising the limit moves the crash into the driver heap |
| Executor heap | `Java heap space`, or `GC overhead limit exceeded` under the parallel collector, on a task that fails 4 attempts before the stage aborts | A single task holding a hot key's group, a broadcast table too big for the heap, or too many tasks sharing one heap | Shrink the oversized task, use fewer cores per executor, stop broadcasting the big side |
| Execution pool | `SparkOutOfMemoryError: Unable to acquire 65536 bytes of memory, got 0` | A task needed another page and nothing could spill to free one | More shuffle partitions, or fewer concurrent tasks per executor |
| Container limit | Executor lost with exit code 137 or 143 and no stack trace | Memory outside the heap: Python workers, network buffers, an unbudgeted off-heap pool | Raise `spark.executor.memoryOverhead`, or budget off-heap memory |
| Direct buffers | `OutOfMemoryError: Direct buffer memory` while reading shuffle blocks | Network buffers for many large fetches in flight at once | Lower `spark.reducer.maxSizeInFlight` from its 48m default, raise overhead |
Case: the hot-key list
Back in the pricing job, a stage builds each rider's list of trip events with collect_list. Most tasks finish in seconds. One fails with Java heap space, is retried 3 times, and the stage aborts on the 4th failure. The animation below plays the same grouping on 12 events and shows why no retry can succeed.
lists = events.groupBy("user_id").agg({"event_type": "collect_list"})
lists.collect()| event_id | user_id | event_type |
|---|---|---|
| 1 | 7 | view |
| 2 | 3 | view |
| 3 | 7 | click |
| 4 | 7 | view |
| 5 | 5 | view |
| 6 | 7 | cart |
| 7 | 7 | view |
| 8 | 9 | click |
| 9 | 7 | view |
| 10 | 7 | click |
| 11 | 3 | cart |
| 12 | 7 | view |
The shuffle sends every row for a key to the same reduce task. User 7 owns 8 of the 12 events, so one task assembles a list twice the size of all the others combined, while the remaining lists stay short. Scaled up to a rider with millions of events, that single task holds the whole list in its heap, and every retry hands the same rider to a task with the same heap. Raising executor memory postpones the failure until that rider's list grows again. The first fix changes what the task must hold: aggregate the hot rider to counts or a capped sample instead of a full list, or split the hot key off and process it on its own path.
Case: Exit Code 137
Later the same night, executors start disappearing with exit code 137. Their tasks show peak execution memory well under each task's share and no Java error at all. Nothing inside the heap ran out: the process as a whole crossed its container's limit, and the cluster manager killed it. The first fix is a larger spark.executor.memoryOverhead. If the job also enables off-heap memory, confirm next that the container budget includes spark.memory.offHeap.size alongside heap and overhead.
Case: the result that came home
The morning report ends with collect() on 9 million priced rows, and the driver stops it with the maxResultSize error at the 1g default. Raising the limit to 2g lets more bytes through and moves the failure into the driver heap, which must hold every row as an object once they arrive. The first fix keeps the rows on the cluster: write the priced table out, and let the report read a summary of it.
A stage loses 3 executors, each reported with exit code 143 and no Java stack trace. Their tasks peak at about half their execution memory share without spilling, and task sizes are even across the stage. What is the best first fix?
- Raise spark.executor.memory, so every memory region inside the executor grows
- Raise spark.sql.shuffle.partitions, so each task needs less execution memory
- Raise spark.executor.memoryOverhead, since the process outgrew its container
- Salt the join key, since a single task must be holding a hot key's rows
About this lesson
This section is part of the Memory and Caching: Advanced 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.