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

WhereWhat you seeUsual causeFirst fix
Driver heap`OutOfMemoryError: Java heap space` on the driver, ending the applicationA large `collect()`, or building a broadcast tableKeep 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 defaultThe 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 abortsA single task holding a hot key's group, a broadcast table too big for the heap, or too many tasks sharing one heapShrink 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 oneMore shuffle partitions, or fewer concurrent tasks per executor
Container limitExecutor lost with exit code 137 or 143 and no stack traceMemory outside the heap: Python workers, network buffers, an unbudgeted off-heap poolRaise `spark.executor.memoryOverhead`, or budget off-heap memory
Direct buffers`OutOfMemoryError: Direct buffer memory` while reading shuffle blocksNetwork buffers for many large fetches in flight at onceLower `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()
events
event_iduser_idevent_type
17view
23view
37click
47view
55view
67cart
77view
89click
97view
107click
113cart
127view

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?

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.