Skip to content

Memory Utilization ​

MEM

What it is ​

An executor's memory is a fixed allocation. spark.executor.memory sets the amount of memory per executor process, which is its JVM heap1. On YARN and Kubernetes the container or pod also carries spark.executor.memoryOverhead on top, plus any off-heap and PySpark memory1. The executor holds the whole allocation for its entire lifetime, whether or not it has work to run2. This finding checks how well that allocation is used, from three angles: how close each executor's heap came to its limit, how much allocated memory-time went unused over the run, and how many cores sat idle in held executors.

The heap check reads Spark's peak heap metric. Executor metrics are sent from each executor to the driver with its heartbeat, and the per-executor peak of JVMHeapMemory is the highest used heap the executor reported. "Used" there counts "the amount of memory occupied by both live objects and garbage objects that have not been collected"3. The finding compares that peak with spark.executor.memory. The metric reaches the event log only when spark.eventLog.logStageExecutorMetrics is true3.

The core check is about concurrency. An executor's core count is its concurrency ceiling: --executor-cores 5 caps it at five concurrent tasks4. The scheduler turns cores into task slots from spark.executor.cores and spark.task.cpus, which defaults to one core per task1. Cores go idle whenever there are fewer runnable tasks than slots: a stage with fewer partitions than total slots leaves slots empty, and so does the tail of a stage where a straggler or two keep running after their peers finish. Through all of it the JVM keeps its fixed heap2.

How it's detected ​

RuleFires whenStatus
heapNearCapacityAn executor's peak heap is above 95% of spark.executor.memory (memory may be too small)Validated
heapOverProvisionedAn executor's peak heap is below 70% of spark.executor.memory (memory may be over-provisioned, a cost signal)Validated
idleCoresMore than 50% of the available core-time ran no taskValidated
wasteModelAllocated memory-time exceeds used memory-time by more than 2.5 times (wasted memory-time, allocated minus used, above 1.5 times used)Experimental

Both heap rules look at executors one at a time, so a run can produce one finding per executor. Driver memory is not evaluated. If the log carries no per-executor memory metrics, the heap rules cannot run, and the finding says so and points at spark.eventLog.logStageExecutorMetrics instead.

Why it matters ​

The two heap bands are opposite problems.

A heap near its limit leaves tasks little room. When a sort or hash-aggregation operator keeps asking for execution memory it cannot get, Spark blocks the task, spills its in-memory data to disk, or in the worst case throws an OutOfMemoryError2. A full GC invoked several times before a task completes means there isn't enough memory for executing tasks5.

A heap far below its limit is the opposite: allocation you pay for without using it. On YARN that is the memory YARN grants the container; on Kubernetes it is the pod memory limit2. Idle cores and lingering executors cost the same way, since the fixed band is reserved whether or not tasks are running.

How to fix it ​

Heap near capacity ​

Check first whether the pressure is real. The peak counts uncollected garbage, so a high reading alone does not prove a shortage3. Look at spill and GC pressure for the same executor and stages. Spill, or heavy GC with repeated full collections, points at a real shortage5.

If it is real, there are three levers:

  • Raise spark.executor.memory. The container request is the sum of spark.executor.memoryOverhead, spark.executor.memory, spark.memory.offHeap.size and spark.executor.pyspark.memory, so the cluster must have room for the larger total1.
  • Shrink what each task holds. When the working set of a task is too large, such as a reduce task building a hash table, the simplest fix is to raise the level of parallelism so each task's input set is smaller5.
  • Run fewer tasks per executor. Each task may take roughly between 1/(2n) and 1/n of execution memory with n tasks running, so lowering spark.executor.cores raises each task's share2.

A heap finding does not cover memory outside the heap. A container killed for exceeding its memory limit shows up as a container-memory error rather than a Spark exception, and the fix there is overhead sizing, covered on the memory management page2.

Heap over-provisioned ​

Lower spark.executor.memory toward the observed peak, in steps, and re-check spill and GC after each change. The unified execution and storage region is a fraction of the heap (spark.memory.fraction of the heap minus 300 MiB), so a smaller heap shrinks it in proportion5. Because per-task execution memory shrinks with it, a heap cut too far trades the cost signal for spill2. Cached data lives in that region too, so a job that persists large datasets needs the headroom its cache will use.

Idle cores and held executors ​

Keep executors from being oversized in the first place. Assigning all 16 cores of a node to a single executor hurts HDFS throughput and drives excessive garbage collection, so aim for a balance between tiny (one core per executor) and fat (one executor per node) sizing6. On YARN, leave cores for the OS and Hadoop daemons instead of handing 100% of a node to Spark containers4.

For the held memory band, lean on dynamic allocation to reclaim executors once the work drains. It requests executors when tasks back up and frees them when they go idle4. Executors are added in rounds once tasks have been pending for spark.dynamicAllocation.schedulerBacklogTimeout (default 1s), then again every spark.dynamicAllocation.sustainedSchedulerBacklogTimeout while the backlog holds7. On the release side, an executor is removed after it has been idle longer than spark.dynamicAllocation.executorIdleTimeout7.

Cached data is the trap here. By default an executor holding cached blocks is never removed, governed by spark.dynamicAllocation.cachedExecutorIdleTimeout, whose default is infinity1. Such an executor keeps its full memory band indefinitely with no active tasks unless you set that timeout to a finite value, or turn on spark.shuffle.service.fetch.rdd.enabled so executors holding only disk-persisted blocks are treated as idle after spark.dynamicAllocation.executorIdleTimeout and released7. To avoid holding barely-used executors at all, lower spark.dynamicAllocation.executorAllocationRatio (default 1.0, full parallelism) toward 0.5, since with small tasks full-parallelism allocation can request executors that never do any work1.

properties
# Balanced executor sizing, not one fat executor per node
spark.executor.cores=5

# Reclaim idle executors; give cached holders a finite timeout
spark.dynamicAllocation.enabled=true
spark.dynamicAllocation.cachedExecutorIdleTimeout=300s
spark.dynamicAllocation.executorAllocationRatio=0.5

Reading the wasted-memory estimate EXPERIMENTAL ​

The wasteModel rule compares two memory-time figures. Allocated memory-time is the executor memory multiplied by the number of executors and the run duration. Used memory-time approximates the allocation as in use for as long as tasks ran, taking executor memory multiplied by the total task run time. The rule fires when that difference, the wasted memory-time, exceeds 1.5 times the used figure, which means allocated memory-time is more than 2.5 times used. Treat it as a rough buffer heuristic, not a measurement. It prices time with no task running, not bytes of heap left empty, so it can disagree with the heap bands: an executor can be busy all run with a half-empty heap, or idle with a full one. Use the number as a hint that the cluster may be oversized or underused, then confirm against sizing before acting on it.

Confidence ​

The idleCores rule and both heap rules are validated: they rest on documented Spark concurrency, dynamic-allocation and heap-metric behavior. The wasteModel estimate is low-confidence and experimental. It is a buffer heuristic derived from allocated-versus-run-time accounting, not an exact measure of unused memory, so it should steer investigation rather than settle it.

Limitations / false-positive risk ​

The heap peak counts garbage the JVM has not collected yet3, so heapNearCapacity can fire on an executor that never ran short of memory. Confirm with spill and GC time before raising memory. The peak is also only as fine-grained as the metric polling. With spark.executor.metrics.pollingInterval at its default of 0, metrics are collected on executor heartbeats, at spark.executor.heartbeatInterval1, so a short spike between heartbeats can go unseen and heapOverProvisioned can understate real use. The heap ratio sees only the heap: overhead, off-heap and PySpark memory are outside it1.

Idle cores are not always waste. The tail of a stage legitimately leaves slots empty while a straggler or two finish1, so a snapshot of low parallelism can reflect a normal straggler tail rather than chronic under-utilization. Executor metrics are absent entirely unless spark.eventLog.logStageExecutorMetrics is enabled3.

Managed platforms: Several platforms set or replace the executor sizing and scaling this page discusses. Databricks autoscaling replaces spark.dynamicAllocation.enabled, and serverless compute does not support most Spark properties.8 On Amazon EMR, maximizeResourceAllocation sets spark.executor.memory and related spark-defaults values from the instance type,9 and managed scaling expects Spark dynamic allocation to stay enabled.10 AWS Glue memory is fixed by worker type, 16 GB for G.1X and 32 GB for G.2X, with one executor per worker.11

Native engines: Apache Gluten and Apache DataFusion Comet cover native memory sizing, which the heap ratio does not see.


  1. Configuration — Spark ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  2. Dive into Spark memory ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  3. Monitoring — Spark ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  4. How to Tune Your Apache Spark Jobs (Part 2) ↩︎ ↩︎ ↩︎

  5. Tuning Spark ↩︎ ↩︎ ↩︎ ↩︎

  6. Distribution of executors, cores and memory for a Spark application ↩︎

  7. Job Scheduling — Spark ↩︎ ↩︎ ↩︎

  8. Set Spark configuration properties on Databricks ↩︎

  9. Configure Spark — Amazon EMR ↩︎

  10. Using managed scaling in Amazon EMR ↩︎

  11. Adding jobs in AWS Glue ↩︎