Skip to the page
Tecyfy

Spark out of memory: read the error, then fix the right thing

A Spark job that runs out of memory fails with one of 4 errors, and each needs a different fix: Java heap space on an executor, Container killed by YARN, maxResultSize on the driver, or a driver heap error after collect(). Find which you have, then change the one setting or line of code that matches.

Tecyfy · Published · 9 min read

A chalk drawing of a bucket labelled OOM overflowing with sparks that spill onto the ground

When Spark runs out of memory, the error message tells you which memory ran out, and each kind has its own fix. An executor heap error can mean one task holds too much data, and then the fix is more partitions or a skew fix, not more memory. "Container killed by YARN" or exit code 137 means memory outside the heap ran out, so raise the overhead. A maxResultSize error or a driver heap error means your code pulled too much back to the driver, so stop collecting.

Find your error

Pick the line that matches your log, and the triage shows where the memory went and what to change first:

Try itSpark out of memory: which memory ran out?
The error
8GiB

java.lang.OutOfMemoryError: Java heap space

Executor container: 8.8 GiB

  • Reserved 300 MiB
  • Unified 4.6 GiB
  • User 3.1 GiB
  • Overhead 819 MiB (outside the heap)

Driver

1 GiB heap

by default

Fix in this order Executor

  1. 1More shuffle partitions, or repartition()
  2. 2Fix a skewed key
  3. 3groupByKey → an aggregation
  4. 4Lower the broadcast threshold
  5. 5Then more executor memory

One task's working set is too big for the heap. First fix: more shuffle partitions, or repartition(). With 8 GiB of executor memory, tasks share about 4.6 GiB of unified memory and the container asks for 8.8 GiB.

Default settings: 300 MiB reserved, 60% of the rest unified memory, 40% user memory, overhead 10% of executor memory with a 384 MiB floor.

Where executor memory goes

An executor is a JVM process inside a container. With the default settings, a spark.executor.memory of 8 GiB means:

PartSize with 8 GiBWhat uses it
Reserved300 MiBSpark itself
Unified memory, 60% of the restabout 4.6 GiBShuffles, joins, sorts, aggregations and cached data, sharing one pool
User memory, the other 40%about 3.1 GiBYour objects, UDF data, Spark's metadata
Overhead (outside the heap)819 MiB (10%, at least 384 MiB)Thread stacks, network buffers, native libraries
Python workersinside the overhead unless spark.executor.pyspark.memory is setpandas UDFs, Python UDFs

The container asks for heap plus overhead. That is why the 2 kinds of error differ: a full heap gives Java's OutOfMemoryError, and a container over its total gives YARN's or Kubernetes' kill. The executor memorySpark Executor MemoryThe memory each Spark worker process gets: a Java heap, split into a shared pool for shuffles, joins and cached data and a pool for your own objects, plus overhead outside the heap. concept page explains the pools in more depth.

1. Java heap space on an executor

java.lang.OutOfMemoryError: Java heap space

in an executor's log, or a task that fails and retries until the stage fails. A common cause is that the working set of one task is too big, as with groupByKey, which gathers every value for a key in memory. Fix it in this order:

  1. Smaller tasks. Raise spark.sql.shuffle.partitions above its default of 200 for a large shuffle, or repartition() before the heavy step. Our shuffle partition calculator gives a number.
  2. One huge key. If one task fails while the rest finish in seconds, it is skewShuffle and Data SkewA shuffle moves rows between machines so rows with the same key meet, for a join or group by; skew is when one key has far more rows, so one task does most of the work.: handling skewed data shows salting and adaptive skew joins.
  3. A better operation. Replace groupByKey with reduceByKey or a DataFrame aggregation, which combine values before the shuffle.
  4. A broadcast that is too big. A table broadcast to every executor must fit in each one. If a broadcast join fails, set a lower spark.sql.autoBroadcastJoinThreshold or remove the broadcast() hint.
  5. Then memory. Only now raise spark.executor.memory.

2. Container killed by YARN, or exit code 137

Container killed by YARN for exceeding memory limits.

The heap may be half empty: what went over is the total, mostly memory outside the heap. AWS's own order of fixes for EMR:

  1. Raise the overhead gradually, up to about 25% of executor memory: spark.executor.memoryOverhead=2g.
  2. Fewer cores per executor, so fewer tasks share its memory at once.
  3. More partitions.
  4. More executor memory, keeping memory plus overhead under what the machine gives a container.

PySpark jobs hit this more than Scala ones, because Python workers live outside the heap. On Kubernetes, Spark already sets the overhead factor to 40% for non-JVM jobs. Elsewhere, set spark.executor.pyspark.memory to limit each executor's Python memory to that amount; on YARN and Kubernetes it is added to the container request. The limit does not work on Windows and is not enforced on macOS.

3. Too much sent to the driver

Job aborted due to stage failure: Total size of serialized results of 1520 tasks (1024.3 MB)
is bigger than spark.driver.maxResultSize (1024.0 MB)

Or OutOfMemoryError in the driver's log. Both come from actions that bring data back to one machine: collect(), toPandas(), a huge show(), or a broadcast built on the driver. The driver has 1 GiB of heap by default.

# Before: every row lands in the driver's memory
rows = df.filter("event_date = '2026-10-21'").collect()

# After: the executors write it, and the driver stays light
df.filter("event_date = '2026-10-21'") \
  .write.mode("overwrite").parquet("s3://bucket/exports/2026-10-21/")

# If you need a sample on the driver, take one
sample = df.limit(1000).toPandas()

Raising spark.driver.maxResultSize and spark.driver.memory does work, but Spark's documentation warns that a high limit can make the driver itself run out of memory. Treat it as a stopgap.

4. GC overhead and slow death

java.lang.OutOfMemoryError: GC overhead limit exceeded

The JVM spent nearly all its time collecting garbage and freed almost nothing. It is the same problem as the heap error, seen earlier: too many objects. Spark's tuning guide notes that garbage collection costs grow with the number of objects, so the fixes above apply, plus 2 more: use DataFrames rather than RDDs of Python or Java objects, and switch to Kryo serialization if you still use RDDs.

Before you change a setting

  • Find the failing stage in the Spark UI. The Stages tab shows peak memory and spill per task. One task far above the median is skew; all tasks high means partitions are too big.
  • Spill is a warning. Large "spill (disk)" numbers mean tasks ran short of memory and wrote data to disk, so a bigger input may fail.
  • Check the machine. More memory per executor means fewer executors per machine. Our Spark cluster sizing calculator shows the trade.

On Databricks the same errors appear, with the driver and worker sizes set by the cluster's instance types. For the maxResultSize error, Databricks adds 2 options: a driver instance with more memory, and Cloud Fetch for BI tools that read results over JDBC or ODBC, which lets them download results from cloud storage instead of through the driver.

Questions

How do I fix java.lang.OutOfMemoryError Java heap space in Spark?
First find where it happened. On an executor it can mean one task holds too much data at once: increase the number of partitions, fix data skew, or replace groupByKey with a reduce or aggregation. Only then raise spark.executor.memory. On the driver it usually means collect() or toPandas() pulled too much data back: write the result to storage instead, or raise spark.driver.memory.
What does Container killed by YARN for exceeding memory limits mean?
The executor's total memory, its Java heap plus the memory outside the heap, went over what YARN gave the container. The heap was not necessarily full. Raise spark.executor.memoryOverhead gradually (its default is 10% of executor memory, at least 384 MiB), and if that does not help, run fewer cores per executor or more partitions.
What is spark.driver.maxResultSize?
The largest total size of serialized results that an action such as collect() may bring back to the driver, 1g by default. A job that exceeds it is aborted with Total size of serialized results ... is bigger than spark.driver.maxResultSize. The safer fix is to stop sending that much data to the driver, not to raise the limit.
Why does PySpark run out of memory when the JVM does not?
Python workers run outside the JVM heap. A pandas UDF or a Python UDF that builds large objects uses memory counted against the container's overhead, not spark.executor.memory. Set spark.executor.pyspark.memory to cap it, or raise the overhead.
Should I just increase executor memory?
Rarely first. An executor out-of-memory error can come from one task's working set being too big, and the simplest fix for that is more partitions, not more memory. More memory per executor also means fewer executors per machine.