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 memoryCoresTasks at onceHeap per task, at most
1g (the default)111GB
16g228GB
16g444GB
16g882GB
32g884GB

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?

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?

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.