Off-Heap Memory
The collector only traces objects on the heap. Spark's memory manager can also allocate native memory outside the heap for the binary rows its operators already use, and the collector neither traces data kept there nor copies it between generations. Two settings create an off-heap pool, which Spark runs as a second unified pool with the same execution and storage split as the one on the heap.
spark = (
SparkSession.builder
.config("spark.executor.memory", "16g")
.config("spark.memory.offHeap.enabled", "true")
.config("spark.memory.offHeap.size", "8g")
.getOrCreate()
)What moves off the heap
The switch moves the execution memory of Tungsten, Spark SQL's engine for operators that work on binary rows in pages Spark allocates itself, as a whole: every sort buffer built from binary rows, and every hash table that aggregations and joins build from them, now takes its pages from the off-heap pool. The heap's execution share keeps serving only the older object-based paths, such as RDD aggregations, and on-heap caching can borrow what they leave idle. So the heap should shrink when off-heap memory is added, or most of its execution share sits unused.
- Moves off the heap
- Binary-row sort buffers, plus the hash tables aggregations and joins build
- Shuffle write buffers filled by those operators
- Blocks persisted with StorageLevel.OFF_HEAP, always serialized, falling back to disk
- Stays on the heap
- DataFrames cached with cache(), whose default level stores on the heap
- Broadcast tables and the objects tasks create
- RDD aggregations, which buffer JVM objects
- Task bookkeeping and query plan metadata
The off-heap pool follows the same spark.memory.storageFraction as the heap: 4,096MB starts as protected storage, and execution can borrow whatever storage leaves free.
Budgeting the whole container
Off-heap memory sits outside -Xmx, so the heap limit cannot cap it. Spark's own accounting keeps it within spark.memory.offHeap.size, and the container needs room for that much on top of everything else. Spark 3 adds the off-heap size to the executor's container request as a separate amount; Spark 2 expected it to fit inside spark.executor.memoryOverhead, so a config carried over from an old job can count it twice or not at all. Here is the full budget for the executor above, with overhead at its default of 10% of the heap.
| Region | Size | What it holds | Traced by the collector |
|---|---|---|---|
| Reserved | 300MB | Spark's internal objects | Yes |
| User memory | 6,434MB | Objects your code and Spark's metadata create | Yes |
| Unified pool, on heap | 9,650MB | On-heap caching and object-based execution | Yes |
| Off-heap pool | 8,192MB | Execution pages and OFF_HEAP blocks, split 4,096MB each way | No |
| memoryOverhead | 1,638MB | Thread stacks, network buffers, Python worker processes | No |
| Container | 26,214MB | The limit the cluster manager enforces | Not applicable |
The first 3 rows add up to the 16,384MB heap. Adding the 8,192MB pool while keeping a 16g heap grows each container by that full amount, and nodes already packed with these executors now fit fewer. The useful version of this change lowers spark.executor.memory at the same time: a 10g heap beside the 8g pool requests 19,456MB, only 1,434MB more than the 16g heap needed alone, and the collector now traces 10GB instead of 16GB.
Off-heap memory pays off when collection time comes from Spark-managed data: large sort and aggregation buffers, or a big serialized cache, on a heap the collector struggles to keep up with. It does nothing for pressure from broadcast tables or task objects, which stay on the heap. And every off-heap block is serialized, so reading it costs a decode that an on-heap deserialized cache skips.
An executor on Spark 3 is configured with spark.executor.memory set to 20g, default overhead, spark.memory.offHeap.enabled true and spark.memory.offHeap.size 6g. How much memory does its container request?
- 20,480MB, because the off-heap pool and overhead are both carved out of the 20g heap
- 22,528MB, the heap plus 2,048MB of overhead, with off-heap memory left uncounted
- 26,624MB, the heap plus 6,144MB of off-heap memory, with no overhead on top
- 28,672MB, the heap plus 2,048MB of overhead plus 6,144MB of off-heap memory
About this lesson
This section is part of the Memory and Caching: Advanced 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.