Memory per Core
Concepts covered: Memory Management
Every task an executor is running draws from that executor's 1 heap. Put 4 cores in an executor and 4 tasks share its memory; put 8 cores in it and 8 tasks share the same amount. The number that decides whether a task has room is the executor's memory divided by the tasks running on it, which, with the default of 1 core per task, is memory divided by cores.
How Spark splits the heap between tasks
Spark gives memory to running tasks from a pool it sets aside inside the heap. It caps each task at 1/N of that pool, where N is the number of tasks running on the executor right now, and a task that cannot get at least 1/(2N) waits until another task releases some. With 4 tasks running, each holds at most a quarter of the pool; when 3 of them finish and 1 is left, that last task may grow into the whole pool.
The pool is smaller than the heap, since Spark also keeps room for cached data and its own bookkeeping. The table below divides the whole heap to keep the ratio visible, so read each figure as an upper bound on what 1 task can hold.
| Executor memory | Cores | Tasks at once | Heap per task, at most |
|---|---|---|---|
| 1g (the default) | 1 | 1 | 1GB |
| 16g | 2 | 2 | 8GB |
| 16g | 4 | 4 | 4GB |
| 16g | 8 | 8 | 2GB |
| 32g | 8 | 8 | 4GB |
An executor has spark.executor.memory at 24g and spark.executor.cores at 6. One task needs about 5GB of working memory for its partition. What happens while all 6 cores are busy?
- The task is capped near a sixth of the pool, under 4GB, so it cannot hold its 5GB at once.
- The task borrows heap from an idle executor elsewhere until it has the 5GB it needs.
- The task can use the whole 24GB, because all tasks on 1 executor share a single heap.
- The task gets its 5GB, because the 1/N cap applies only to cached data on the executor.
More cores, less memory each
Say the nightly rollup runs well on 16g executors with 4 cores. Someone raises the cores to 8 to finish sooner and leaves the memory alone. Slots double, and every task's ceiling drops from 4GB to 2GB. Tasks that read and filter keep working. Tasks on the reduce side of the shuffle that used up to 4GB now spill: when a task reaches its ceiling, Spark writes part of its working data to local disk and reads it back later. The job still finishes, but the stage slows down and can give back the time the extra slots were meant to save. Data a task cannot write out in pieces, such as a very large row, can make it fail with an out-of-memory error instead, and a task that fails the same way on every retry takes the job down with it.
The change that keeps the extra slots is to raise memory along with the cores: 32g with 8 cores puts every ceiling back at 4GB. Memory per core is the ratio to hold steady whenever an executor's shape changes.
Which tasks need the room
A task's memory need comes from what it does with its partition. A 128MB Parquet partition is compressed on disk, so the same rows take more space once a task decodes them. A filter or a projection handles rows one after another and holds few of them at a time. A groupBy that sums or counts keeps 1 running total per key, so its need grows with the number of distinct keys a task receives, and 1 million rows of a single key still share 1 total. A join builds a lookup table from one side's rows and a sort holds rows until it can order them, so both keep every row they receive, and the reduce side of a join or a sort is where the per-core ratio gets tested hardest.
Giving each task more memory
Raise spark.executor.memory, or run fewer tasks at once on the same heap. Fewer tasks comes from lowering spark.executor.cores, or from raising spark.task.cpus, which sets how many cores each task claims and defaults to 1. An executor with 8 cores and spark.task.cpus at 2 runs 4 tasks at a time, each with up to a quarter of the pool.
Executors run with 16g and 8 cores, and grouping tasks are running out of memory. Which single change raises the memory ceiling of each running task?
- Raise spark.executor.instances from 20 to 40 so the work spreads wider.
- Raise spark.driver.memory so the driver can hold the grouped rows.
- Lower spark.executor.cores from 8 to 4, keeping 16g per executor.
- Raise spark.sql.shuffle.partitions so each reduce task gets a bigger share.
About this lesson
This section is part of the Cluster Sizing: Beginner 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.