The Executor Memory Split
Every executor runs as a single JVM process, and the memory it gets is fixed when it launches. The spark.executor.memory setting, which --executor-memory sets on spark-submit, becomes that JVM's maximum heap. Inside the heap, Spark draws 3 regions, each with its own job and its own way of failing. A further pool, the overhead, sits outside the heap and gets its own section later in this lesson.
Three regions inside the heap
Bottom of the heap to top
- Reserved memory: Spark sets this aside for its own internal objects before counting anything else. No setting changes it, and Spark refuses to launch an executor with less than 450 MB of heap, one and a half times the reserved block.
- User memory: Space outside Spark's accounting. It holds Spark's internal metadata, such as the records it keeps for partitions and blocks, plus any objects that code outside Spark's operators builds on the heap. Nothing here can spill or be evicted, so overfilling it ends in a heap OutOfMemoryError.
- Unified memory: The region Spark manages, sized by spark.memory.fraction. Execution and storage share it, with a soft boundary between them placed by spark.memory.storageFraction, which defaults to 0.5.
Sizing an 8 GB executor
The arithmetic runs in megabytes of 1,024 KB, which the Spark UI writes as MiB: start from the heap, take away the reserved block, then split the rest by spark.memory.fraction. The executor below, 8 GB of heap and 4 cores, is the running example for every section that follows.
| Region | How it is computed | Size at an 8 GB heap |
|---|---|---|
| Heap (`spark.executor.memory`) | heap in GB times 1,024 | 8,192 MB |
| Reserved | fixed | 300 MB |
| Usable heap | heap minus reserved | 7,892 MB |
| Unified (`spark.memory.fraction` 0.6) | usable times 0.6 | 4,735 MB |
| Storage side (`storageFraction` 0.5) | unified times 0.5 | about 2,368 MB |
| Execution side | unified minus storage side | about 2,367 MB |
| User memory | usable times 0.4 | 3,157 MB |
Look at where the 0.6 lands. It multiplies the usable heap, so an executor launched with 8 GB gives Spark's managed region about 4.6 GB, a little over half of what the command line asked for. The storage and execution sides are a starting position: the next section shows each one growing into the other's space, and what happens when the owner wants it back.
Spark 1.6 introduced this layout with spark.memory.fraction at 0.75. Spark 2.0 lowered the default to 0.6, where it has stayed.
Which setting moves which region
| Setting | Default | Raising it does this |
|---|---|---|
| `spark.executor.memory` | 1g | Grows every region except reserved, in proportion |
| `spark.memory.fraction` | 0.6 | Grows unified memory and shrinks user memory by the same amount |
| `spark.memory.storageFraction` | 0.5 | Widens the cache's protected share and narrows execution's |
| `spark.executor.cores` | 1 on most cluster managers | Changes no region, but more tasks share the execution side at once |
An executor launches with spark.executor.memory set to 4g and every other memory setting at its default. How large is its unified region, the pool execution and storage share?
- About 2,458 MB
- About 1,139 MB
- About 2,278 MB
- About 1,518 MB
About this lesson
This section is part of the Memory and Caching: Intermediate 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.