Databricks Serverless Webinar
Watch On-Demand
Close navigation menu icon in white

Each Spark executor's heap is split into 300MB of reserved memory, user memory, and a unified region (60% of the rest by default) shared by execution and storage. Memory outside the heap, called overhead, defaults to 10% of executor memory or 384MB, whichever is larger. Memory per task is the execution memory divided by the cores per executor.

Below: every memory region in one table, a small experiment that shows why cores per executor matter so much, and a worked example for sizing executors, the driver and shuffle partitions. If you're chasing a specific out-of-memory error, start with Part 1: Memory Management.

How Spark divides executor memory

RegionSize (defaults)What it holdsSetting
Reserved300MB, fixedSpark internalsNone
User memory40% of (heap minus 300MB)Your data structures, UDF objects, internal metadataspark.memory.fraction (sets the other 60%)
Unified: executionShares 60% of (heap minus 300MB)Shuffles, joins, sorts, aggregationsspark.memory.fraction = 0.6
Unified: storageShares the same 60%, half protectedCached data, broadcast variablesspark.memory.storageFraction = 0.5
Overheadmax(384MB, 10% of executor memory)Off-heap JVM needs, native libraries, Python workers if not cappedspark.executor.memoryOverhead or spark.executor.memoryOverheadFactor
Off-heap0 unless enabledTungsten off-heap dataspark.memory.offHeap.enabled, spark.memory.offHeap.size
PySpark memoryUnset unless configuredPython worker processesspark.executor.pyspark.memory

The heap is what you set with spark.executor.memory. The container the cluster manager gives each executor is bigger: heap + overhead + off-heap + PySpark memory.

On Kubernetes, the overhead factor is 0.40 for non-JVM jobs such as PySpark, since Python workers live outside the heap.

Spark executor memory regions for a 900MB heap: reserved, user memory, execution and storage

How execution and storage share memory

Execution and storage aren't fixed halves. They borrow from each other:

  • If nothing is cached, execution can use the whole unified region.
  • If nothing is executing, storage can use all of it.
  • Execution can evict cached data, but only until storage drops to its protected half.
  • Storage can never evict execution memory.

So a job that caches nothing gets the full 60% for shuffles and joins. A job that caches heavily still keeps half of it for execution.

Why cores per executor change memory per task

Here's a small experiment that shows the most common memory mistake: adding cores without adding memory.

Setup: a 1GB text file with 10 lines. Each line is one digit repeated 100 million times, so each line is about 100MB on disk. One local executor, 3 cores (local[3]), and a 900MB heap.

Result: out-of-memory error within seconds.

Why. Start with the usable memory:

  • Unified memory = (900MB - 300MB) x 0.6 = 360MB
  • Nothing is cached, so execution gets all 360MB
  • 3 cores means 3 tasks share it: 360MB / 3 = 120MB per task

Now the record size. Each line becomes a Java String, and Java stores each character in 2 bytes, so a 100MB line takes about 200MB in memory. That's the memory expansion rate: about 2x here.

A 200MB record doesn't fit in 120MB. The task fails.

Execution memory taking all of storage memory, with three tasks at 120MB each, too small for a 200MB record

Fix 1: one core. Run with local[1] and the single task gets all 360MB. A 200MB record fits. The job succeeds, but with no parallelism.

One task using all 360MB of execution memory, enough for a 200MB record

Fix 2: smaller records. With 3 cores, each record can be at most 120MB in memory, or about 60MB on disk. A file with lines 0.6 times as long ran fine. At 0.7 times it failed again.

The lessons:

  • Input size alone doesn't cause out-of-memory errors. Record size in memory, divided across tasks, does.
  • More cores per executor means less memory per task.
  • Estimate a record's size in memory, not on disk. Spark's SizeEstimator.estimate measures an object directly.

[cta-banner]

How to size executors, cores and memory

Work it out per node:

  1. Leave room for the system. Keep about 1 core and some memory per node for the OS and the cluster manager.
  2. Pick cores per executor. About 5 is a common starting point. More cores per executor shares the same memory among more tasks.
  3. Divide the node. Executors per node = usable cores / cores per executor.
  4. Size each container. Container memory = memory available for containers / executors per node.
  5. Subtract overhead. Executor memory = container memory minus overhead, where overhead = max(384MB, 10% of executor memory).

Example (illustrative node with 16 cores and 128GB, about 120GB available for containers):

  • 15 usable cores / 5 cores per executor = 3 executors per node
  • 120GB / 3 = 40GB per container
  • Executor memory of about 36GB plus 3.6GB overhead fits in 40GB
  • Each of the 5 tasks gets about (36GB - 300MB) x 0.6 / 5 ≈ 4.3GB of execution memory

On YARN, check yarn.nodemanager.resource.memory-mb and yarn.scheduler.maximum-allocation-mb. A container bigger than those limits won't start.

Spark executors running inside YARN containers on a node, with memory overhead outside each executor heap

Large heaps on Spark 4. Spark 4.0 runs on JDK 17 by default, which makes G1 the default garbage collector. With very large executor heaps, Spark's tuning guide suggests increasing the G1 region size with -XX:G1HeapRegionSize.

Driver memory

The driver plans the job and collects results. It usually needs less memory than executors, unless your code pulls data back to it.

  • spark.driver.memory sets the driver heap. The default is 1GB.
  • spark.driver.maxResultSize caps how much data actions like collect() can return. The default is 1GB.
  • Driver overhead follows the same rule as executors.

collect(), toPandas() and large broadcast joins all land on the driver. If the driver runs out of memory, see the driver section of Part 1.

[cta-banner]

Sizing shuffle partitions from spill metrics

If a shuffle stage spills, its tasks didn't have enough execution memory. More partitions means smaller tasks.

The Spark UI stage detail shows Shuffle spill (memory) and Shuffle spill (disk) for the same records. Their ratio gives you the memory expansion rate:

  • Memory expansion rate ≈ Shuffle spill (memory) / Shuffle spill (disk)
  • Shuffle size in memory = Shuffle Read x memory expansion rate
  • Shuffle partitions = Shuffle size in memory / execution memory per task

If those numbers are hard to get, a simpler rule: aim for about 150MB of Shuffle Read per task, so partitions = Shuffle Read / 150MB.

Set the result with spark.sql.shuffle.partitions (default 200), or spark.default.parallelism for RDD jobs.

Since Spark 3.2, Adaptive Query Execution coalesces small shuffle partitions automatically. Start with enough partitions to avoid spill, and AQE trims the excess.

Spark UI stage detail showing Shuffle Read, Shuffle spill memory and Shuffle spill disk

Platform notes

  • YARN: each executor runs in a container. Overhead and the YARN limits above decide what fits.
  • Kubernetes: each executor is a pod. Use the 0.40 overhead factor for PySpark and other non-JVM jobs, and remember that tmpfs scratch space counts against pod memory if you enable it.
  • Databricks classic compute: one executor per worker node, using most of the node's memory. You size memory by choosing the node type.
One Spark executor per worker node in standalone mode, using most of the node's memory
  • Databricks serverless: no executor memory settings. Databricks sizes compute for you.

How Unravel fits in

Sizing one job by hand works. Keeping thousands of jobs sized right as data grows doesn't. Arvix, Unravel's autonomous AI operator, resizes compute based on actual workload demand and rewrites inefficient queries across your entire fleet. Learn more about Spark optimization with Unravel, or start with How to Troubleshoot Common Spark Problems.

Get a free health check to find over-allocated Spark jobs.

See Unravel in action.
Book a Demo
Unlock your data environment health with a free health check.
Request Your Health Check Report
Table of Contents