<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Garbage_collection_in_Spark</id>
	<title>Garbage collection in Spark - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Garbage_collection_in_Spark"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Garbage_collection_in_Spark&amp;action=history"/>
	<updated>2026-12-06T00:08:11Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Garbage_collection_in_Spark&amp;diff=150&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Garbage_collection_in_Spark&amp;diff=150&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:42Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Garbage collection in Spark&lt;br /&gt;
| also_known_as   = JVM GC in Spark, GC pressure&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = JVM heap generations, GC time metric, G1GC (the JVM default on server configurations since JDK 9), GC tuning flags&lt;br /&gt;
| implementations = [[Apache Spark]] on the Java Virtual Machine&lt;br /&gt;
| related         = [[Spark memory management]], [[Executor]], [[Caching and persistence]], [[Spill (memory)]], [[Executor memory overhead]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/tuning.md, docs/web-ui.md, docs/monitoring.md, docs/configuration.md, core/src/main/resources/org/apache/spark/ui/static/executorspage.js), the Spark 4.3.0 overview page and OpenJDK JEP 248}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Garbage collection in Spark&amp;#039;&amp;#039;&amp;#039; is the work the Java Virtual Machine (JVM) does inside each [[Apache Spark]] [[executor]] and driver to find objects that are no longer used and free their memory. Spark runs on the JVM, so every task&amp;#039;s objects live on the heap and are cleaned up by the JVM garbage collector (GC). When the heap is under pressure, the GC runs often or for long pauses, and tasks slow down even though they do no extra useful work.&amp;lt;ref name=&amp;quot;tuning&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/tuning.html |title=Tuning Spark (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What does the GC time column in the Spark UI mean? ==&lt;br /&gt;
The Spark UI reports &amp;#039;&amp;#039;&amp;#039;GC time&amp;#039;&amp;#039;&amp;#039; in two places.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Stage page.&amp;#039;&amp;#039;&amp;#039; In the task summary and the task table, GC time is the total time the JVM spent in garbage collection while that task ran.&amp;lt;ref name=&amp;quot;webui&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/web-ui.html |title=Web UI (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The REST API exposes it as &amp;lt;code&amp;gt;jvmGCTime&amp;lt;/code&amp;gt;, in milliseconds.&amp;lt;ref name=&amp;quot;monitoring&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/monitoring.html |title=Monitoring and Instrumentation (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Executors tab.&amp;#039;&amp;#039;&amp;#039; The &amp;quot;Task Time (GC Time)&amp;quot; column shows total task time and GC time per executor.&amp;lt;ref name=&amp;quot;webui&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;exectpl&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/resources/org/apache/spark/ui/static/executorspage-template.html |title=executorspage-template.html (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The UI shades the cell red when GC time is more than 10% of total task time.&amp;lt;ref name=&amp;quot;execjs&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/resources/org/apache/spark/ui/static/executorspage.js |title=executorspage.js (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
GC time is measured per JVM, and several tasks run in one executor JVM at once. A long collection pauses all of them, so tasks that ran at the same time on one executor often show similar GC time. Executor-level metrics also split GC into minor and major collections (&amp;lt;code&amp;gt;MinorGCCount&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MinorGCTime&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MajorGCCount&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MajorGCTime&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;monitoring&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Symptoms of GC pressure ==&lt;br /&gt;
* GC time is a large share of task time, and the Executors tab shows red cells.&amp;lt;ref name=&amp;quot;execjs&amp;quot; /&amp;gt;&lt;br /&gt;
* The Spark docs single out full GCs: if a full GC runs several times before a task completes, there is not enough memory for executing tasks.&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt;&lt;br /&gt;
* Very long pauses can delay the heartbeats that tell the driver an executor is alive. Executors send one every &amp;lt;code&amp;gt;spark.executor.heartbeatInterval&amp;lt;/code&amp;gt; (default 10s), and &amp;lt;code&amp;gt;spark.network.timeout&amp;lt;/code&amp;gt; (default 120s) is the default for the block manager heartbeat timeout.&amp;lt;ref name=&amp;quot;config&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/configuration.html |title=Spark Configuration (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* The executor fails with &amp;lt;code&amp;gt;java.lang.OutOfMemoryError: GC overhead limit exceeded&amp;lt;/code&amp;gt; or a heap OOM. This is different from a container kill; see [[Executor memory overhead]].&lt;br /&gt;
&lt;br /&gt;
== What causes GC pressure? ==&lt;br /&gt;
The Spark docs give two main causes. First, the cost of garbage collection is proportional to the number of Java objects, so data structures with many small objects (a &amp;lt;code&amp;gt;LinkedList&amp;lt;/code&amp;gt; instead of an array of primitives, boxed values, nested collections) cost more. Second, a task&amp;#039;s working memory competes with the data cached on the same executor.&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt; Large deserialized caches, [[Data skew|skewed]] tasks that hold a lot of data at once, and user code that collects large collections in memory all add to the live heap.&lt;br /&gt;
&lt;br /&gt;
DataFrame and SQL operations put less load on the GC than RDDs of Java or Python objects, because Spark SQL keeps rows in a compact binary format and allocates its own memory pages for sorting, hashing and shuffle. See [[Project Tungsten]].&amp;lt;ref name=&amp;quot;tmm&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java |title=TaskMemoryManager.java (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to reduce GC time ==&lt;br /&gt;
The Spark tuning guide recommends these steps, in this order of effort:&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Use serialized caching.&amp;#039;&amp;#039;&amp;#039; Persisting in serialized form leaves one byte array per partition instead of many objects; the docs call this the first thing to try. (In PySpark, cached RDD data is always serialized.)&amp;lt;ref name=&amp;quot;rddguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#rdd-persistence |title=RDD Programming Guide: RDD Persistence (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Cache less.&amp;#039;&amp;#039;&amp;#039; If the old generation is close to full, lower &amp;lt;code&amp;gt;spark.memory.fraction&amp;lt;/code&amp;gt;; it is better to cache fewer objects than to slow down task execution.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Size the young generation.&amp;#039;&amp;#039;&amp;#039; If there are many minor collections but few major ones, give Eden more memory.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Use G1GC settings for large heaps.&amp;#039;&amp;#039;&amp;#039; The JVM that the deployment runs Spark on sets the default collector. Spark 4.3.0 runs on Java 17, 21 or 25,&amp;lt;ref name=&amp;quot;overview&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/ |title=Spark Overview (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; and G1 has been the default collector on server configurations since JDK 9.&amp;lt;ref name=&amp;quot;jep248&amp;quot;&amp;gt;{{Cite web |url=https://openjdk.org/jeps/248 |title=JEP 248: Make G1 the Default Garbage Collector |publisher=OpenJDK |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; With large executor heaps, raising &amp;lt;code&amp;gt;-XX:G1HeapRegionSize&amp;lt;/code&amp;gt; can help.&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
GC flags for executors go in &amp;lt;code&amp;gt;spark.executor.defaultJavaOptions&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;spark.executor.extraJavaOptions&amp;lt;/code&amp;gt;. The heap size itself cannot be set there; it comes from &amp;lt;code&amp;gt;spark.executor.memory&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
spark.executor.memory              16g&lt;br /&gt;
spark.executor.extraJavaOptions    -XX:+UseG1GC -XX:G1HeapRegionSize=32m -Xlog:gc*:file=/tmp/gc-{{EXECUTOR_ID}}.log&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Spark replaces &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;{{APP_ID}}&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;{{EXECUTOR_ID}}&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;spark.executor.extraJavaOptions&amp;lt;/code&amp;gt; with the application and executor IDs, which gives each executor its own GC log.&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Spark memory management]]&lt;br /&gt;
* [[Executor]]&lt;br /&gt;
* [[Executor memory overhead]]&lt;br /&gt;
* [[Caching and persistence]]&lt;br /&gt;
* [[Project Tungsten]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>