Tuning the Garbage Collector
Every executor is a single JVM, and every object its tasks create lives on that JVM's heap until the garbage collector proves nothing still points to it. The collector divides the heap into a young generation, where objects are born and most die within milliseconds, and an old generation, where survivors are promoted. Young collections are frequent and cheap. Reclaiming the old generation means tracing everything still alive there, and the stop-the-world phases of that work freeze every task thread on the executor at once, so a single long pause stalls all of its cores together.
Reading the GC share
Spark records how long each task spent inside garbage collection. The Executors tab shows it inside Task Time (GC Time) and colors that cell red once GC passes 10% of task time, and a stage's task table lists it per task. Below 10%, collection is a minor cost and tuning the collector buys almost nothing. Above 10%, ask why the heap churns, because 2 stages with the same GC share can need opposite fixes.
| Reading | Stage 4 | Stage 7 |
|---|---|---|
| Executor heap | 48GB | 16GB |
| Collector | Parallel, the Java 8 default | G1 |
| Median task time | 45s | 52s |
| Median GC time | 18s | 21s |
| GC share | 40% | 40% |
| Storage memory used | 2.1GB of 28.6GB | 9.3GB of 9.4GB |
| Spill per task | 0 | 1.8GB |
| Failed tasks | 0 | 6, all OutOfMemoryError |
Stage 4 has room to spare: storage holds 2.1GB of 28.6GB, and no task spills or fails. Its lost time is pauses. The parallel collector stops every thread for each old-generation collection, and that work grows with the old generation, so on a 48GB heap a single pause can last seconds. That is a policy problem, and changing the collector fixes it. Stage 7 is a capacity problem. Its storage sits at its limit and its tasks spill, with 6 of them dying. Each collection frees little because nearly everything on the heap is still in use, so the collector runs again almost at once, and no collector setting creates room that live data already fills.
When the collector is the fix
On Java 8 the default collector is Parallel; from Java 9 onward the JVM picks G1 on server-class machines. G1 splits the heap into equal regions and reclaims the old generation a few regions at a time, aiming to hold each pause under -XX:MaxGCPauseMillis, 200ms by default. A large executor heap still on the parallel collector should try G1 first. Once on G1, 3 more flags do most of the remaining work.
spark = (
SparkSession.builder
.config("spark.executor.memory", "16g")
.config(
"spark.executor.extraJavaOptions",
"-XX:+UseG1GC "
"-XX:InitiatingHeapOccupancyPercent=35 "
"-XX:G1HeapRegionSize=16m "
"-XX:ConcGCThreads=4",
)
.getOrCreate()
)What each G1 flag changes
- InitiatingHeapOccupancyPercent=35: G1 starts marking the old generation when the heap is 35% occupied instead of the default 45%, so marking finishes before free regions run out and force a full stop-the-world collection. From Java 9, adaptive tuning treats this as a starting value.
- G1HeapRegionSize=16m: an object at least half a region in size is humongous and goes straight into old regions. A 16GB heap defaults to 8MB regions, making every array of 4MB or more humongous, and Spark allocates many large arrays for execution pages and cached batches. Setting 16m raises the threshold to 8MB, and Spark's tuning guide advises a larger region size for large heaps.
- ConcGCThreads=4: more threads for the marking that runs alongside tasks, so it keeps pace with allocation, paid for in CPU the tasks would otherwise use.
For a capacity problem like Stage 7, shrink what lives on the heap instead. Give each executor fewer cores so fewer tasks share it, or split the work into more partitions so each task holds less. Caching less also helps, and so does moving execution buffers and cached data out of the heap, which the next section covers.
An executor runs a 40GB heap on Java 8 with no collector flags. Its stage shows a 30% GC share and storage memory at 3GB of 23.8GB. No task spills or fails, but every few minutes all its tasks freeze together for several seconds. What is the best first change?
- Raise spark.executor.memory to 56g so that old-generation collections come less often
- Add -XX:+UseG1GC to spark.executor.extraJavaOptions to replace the parallel collector
- Add -Xmx48g to spark.executor.extraJavaOptions to give the old generation more room
- Persist the input with MEMORY_ONLY so tasks stop rereading it and allocate less
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.