GC Logs
The GC Time column in the Executors tab says how much of a task went to garbage collection. It cannot say whether the collector was clearing short-lived garbage at a furious rate or fighting for room in a heap packed with data the job still needs. Those 2 problems call for opposite fixes, and the JVM's GC log separates them: a line per collection, with heap occupancy before and after.
Turning it on
spark = (
SparkSession.builder
.config(
"spark.executor.extraJavaOptions",
"-Xlog:gc:stdout:time,uptime",
)
.getOrCreate()
)The JVM reads this option when the executor launches, so it belongs in the submit command or the builder of a new application; setting it on a running session changes nothing. Sending the log to stdout puts it behind the stdout link in the Executors tab, beside the stderr from the previous section. For long jobs, write to a rotating file instead, -Xlog:gc:file=/tmp/gc-%p.log:time,uptime:filecount=5,filesize=20M, so a week of collections cannot fill a disk that shuffle files also need.
Reading a line
A G1 pause line looks like this: [1201.002s][info][gc] GC(311) Pause Young (Normal) (G1 Evacuation Pause) 6144M->1210M(8192M) 52.104ms. Read it from the right. 52.104ms is the pause, during which every task thread on the executor stood still. 8192M is the heap's capacity. 6144M->1210M is occupancy before and after, so this collection freed 4,934 MB. The after-value matters most: whatever survives a collection is still referenced by something, so it is an upper bound on the data the executor is keeping alive.
Here are 2 executors from the same stage, both with GC Time shown in red in the Executors tab, which marks it once garbage collection passes 10% of task time. Both heaps are 8,192 MB.
Executor 3's after-values hold flat near 1.2 GB, so the data it keeps fits with room to spare. What drives its collections is allocation. Subtract a line's after-value from the next line's before-value and divide by the time between them to get the allocation rate; divide total pause time by elapsed time to get the share spent stopped. Executor 9's after-values climb at every collection, each young pause frees less than the one before, and a full collection that freezes the JVM for 9.3 seconds hands back almost nothing.
- 9.9 GB/s
- 11%
- 97%
Churn or a full heap
- Allocation churn
- After-GC heap low and flat
- Each young pause frees most of the heap
- No full collections
- Cause: code creating short-lived objects for each row
- Fix: allocate less, starting from the frames that allocate most
- Full heap
- After-GC heap climbing toward capacity
- Each pause frees less than the last
- Full collections that free little, then OutOfMemoryError
- Cause: tasks holding more than their share of heap
- Fix: hold less per task, or give each task more heap
More heap would space executor 3's collections further apart, but the code would produce garbage at the same rate, so the lasting fix is in the code path. Common sources are plans where whole-stage code generation fell back to evaluating row by row, RDD functions that build an object per record, and string expressions that copy every value. The flame graph in the next section names the exact frames. Executor 9 needs the reverse: its tasks hold too much at once, so cut what each holds with more shuffle partitions, fewer cores per executor, or by releasing a cache that crowds the heap.
Executor 14 has a 12 GB heap and its GC Time shows red. During a slow stage its GC log prints a pause every 0.4 seconds: 9800M->2110M 71ms, 9820M->2090M 69ms, 9790M->2150M 73ms, 9810M->2100M 70ms. Which reading is right?
- Full heap: about 9.8 GB is in use before each pause, so its tasks hold too much.
- Churn: about 2.1 GB survives each pause, and the code allocates roughly 19 GB a second.
- Full heap: a young pause every 0.4 seconds means the collector cannot keep up with live data.
- Churn: the 70 ms pauses come from GC logging itself, so turn the log off.
About this lesson
This section is part of the The Spark UI: 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.