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.

ReadingStage 4Stage 7
Executor heap48GB16GB
CollectorParallel, the Java 8 defaultG1
Median task time45s52s
Median GC time18s21s
GC share40%40%
Storage memory used2.1GB of 28.6GB9.3GB of 9.4GB
Spill per task01.8GB
Failed tasks06, 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

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?

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.