<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic JVM Heap Leak When Iterating Over Large Number of Tables Using DESCRIBE DETAIL in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/jvm-heap-leak-when-iterating-over-large-number-of-tables-using/m-p/114752#M44933</link>
    <description>&lt;P&gt;&lt;STRONG&gt;Problem:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I'm trying to generate a consolidated metadata table for all tables within a Databricks database (I do not have admin privileges). The process works fine for the first few thousand tables, but as it progresses, the driver node eventually crashes with a JVM heap memory issue.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Goal:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Create a table (e.g., metadata_table) in my_database that stores metadata for each table using DESCRIBE DETAIL.&lt;/P&gt;&lt;P&gt;Additionally, I would like to do the same for historical metadata using DESCRIBE HISTORY, saved to a similar table (e.g., metadata_history_table). I suspect I’ll encounter similar performance and memory issues with that as well.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Current Code (for DESCRIBE DETAIL):&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;database = 'my_database'
tables_df = spark.sql(f"SHOW TABLES IN {database}")
table_names = [row.tableName for row in tables_df.collect()]

for i, table in enumerate(table_names, start=1):
    try:
        df = spark.sql(f"DESCRIBE DETAIL {database}.{table}")
        df = df.withColumn("database", f.lit(database)).withColumn("table_name", f.lit(table))
        df.write.mode('append').saveAsTable('my_database.metadata_table')
    except Exception as e:
        print(f"Error with {table}: {e}")&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Context:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The database contains ~10,000 tables.&lt;/LI&gt;&lt;LI&gt;Processing proceeds fine until about the 3000th table.&lt;/LI&gt;&lt;LI&gt;After that, performance degrades rapidly and the notebook eventually fails with:&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt;Internal error. Attach your notebook to a different compute or restart the current compute.
com.databricks.backend.daemon.driver.DriverClientDestroyedException: abort: Driver Client destroyed&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Observations:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Spark UI shows red "Task Time" for the driver with high "GC Time".&lt;/LI&gt;&lt;LI&gt;Storage Memory does not appear to be under pressure. Screenshot of Spark UI Executors&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="spark_ui.png" style="width: 999px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/15850i5D109C669917E209/image-size/large?v=v2&amp;amp;px=999" role="button" title="spark_ui.png" alt="spark_ui.png" /&gt;&lt;/span&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;JVM heap usage increases steadily with each iteration:&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt;# Logging JVM heap usage
def log_jvm_heap():
rt = spark.jvm.java.lang.Runtime.getRuntime()
used = (rt.totalMemory() - rt.freeMemory()) / (1024 * 1024)
total = rt.maxMemory() / (1024 * 1024)
print(f"[Heap] {used:.2f} MB / {total:.2f} MB")&lt;/LI-CODE&gt;&lt;P&gt;Sample output:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;[Heap] 1328.19 MB / 43215.00 MB # iteration 100
[Heap] 2215.11 MB / 43526.50 MB # iteration 200
...
[Heap] 18718.88 MB / 43526.00 MB # iteration 1600
[Heap] 20008.39 MB / 43495.50 MB # iteration 1700&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;What I’ve Tried:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Explicit memory cleanup after each iteration:&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt;df = None
del df
gc.collect()
spark._jvm.java.lang.System.gc()&lt;/LI-CODE&gt;&lt;UL&gt;&lt;LI&gt;Batching the write every 100 tables to reduce write frequency&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Unfortunately, neither attempt prevents the memory usage from growing.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Questions:&lt;/STRONG&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Is there a reliable way to force JVM heap cleanup in this context?&lt;/LI&gt;&lt;LI&gt;Is this behavior a known limitation when using DESCRIBE DETAIL or DESCRIBE HISTORY iteratively at scale?&lt;/LI&gt;&lt;LI&gt;Are there more memory-efficient or scalable alternatives to gather metadata across many tables without requiring admin privileges?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Any advice or guidance is much appreciated! Thank you!&lt;/P&gt;</description>
    <pubDate>Mon, 07 Apr 2025 19:24:37 GMT</pubDate>
    <dc:creator>unnamedchunk</dc:creator>
    <dc:date>2025-04-07T19:24:37Z</dc:date>
    <item>
      <title>JVM Heap Leak When Iterating Over Large Number of Tables Using DESCRIBE DETAIL</title>
      <link>https://community.databricks.com/t5/data-engineering/jvm-heap-leak-when-iterating-over-large-number-of-tables-using/m-p/114752#M44933</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Problem:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I'm trying to generate a consolidated metadata table for all tables within a Databricks database (I do not have admin privileges). The process works fine for the first few thousand tables, but as it progresses, the driver node eventually crashes with a JVM heap memory issue.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Goal:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Create a table (e.g., metadata_table) in my_database that stores metadata for each table using DESCRIBE DETAIL.&lt;/P&gt;&lt;P&gt;Additionally, I would like to do the same for historical metadata using DESCRIBE HISTORY, saved to a similar table (e.g., metadata_history_table). I suspect I’ll encounter similar performance and memory issues with that as well.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Current Code (for DESCRIBE DETAIL):&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;database = 'my_database'
tables_df = spark.sql(f"SHOW TABLES IN {database}")
table_names = [row.tableName for row in tables_df.collect()]

for i, table in enumerate(table_names, start=1):
    try:
        df = spark.sql(f"DESCRIBE DETAIL {database}.{table}")
        df = df.withColumn("database", f.lit(database)).withColumn("table_name", f.lit(table))
        df.write.mode('append').saveAsTable('my_database.metadata_table')
    except Exception as e:
        print(f"Error with {table}: {e}")&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Context:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The database contains ~10,000 tables.&lt;/LI&gt;&lt;LI&gt;Processing proceeds fine until about the 3000th table.&lt;/LI&gt;&lt;LI&gt;After that, performance degrades rapidly and the notebook eventually fails with:&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt;Internal error. Attach your notebook to a different compute or restart the current compute.
com.databricks.backend.daemon.driver.DriverClientDestroyedException: abort: Driver Client destroyed&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Observations:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Spark UI shows red "Task Time" for the driver with high "GC Time".&lt;/LI&gt;&lt;LI&gt;Storage Memory does not appear to be under pressure. Screenshot of Spark UI Executors&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="spark_ui.png" style="width: 999px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/15850i5D109C669917E209/image-size/large?v=v2&amp;amp;px=999" role="button" title="spark_ui.png" alt="spark_ui.png" /&gt;&lt;/span&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;JVM heap usage increases steadily with each iteration:&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt;# Logging JVM heap usage
def log_jvm_heap():
rt = spark.jvm.java.lang.Runtime.getRuntime()
used = (rt.totalMemory() - rt.freeMemory()) / (1024 * 1024)
total = rt.maxMemory() / (1024 * 1024)
print(f"[Heap] {used:.2f} MB / {total:.2f} MB")&lt;/LI-CODE&gt;&lt;P&gt;Sample output:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;[Heap] 1328.19 MB / 43215.00 MB # iteration 100
[Heap] 2215.11 MB / 43526.50 MB # iteration 200
...
[Heap] 18718.88 MB / 43526.00 MB # iteration 1600
[Heap] 20008.39 MB / 43495.50 MB # iteration 1700&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;What I’ve Tried:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Explicit memory cleanup after each iteration:&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt;df = None
del df
gc.collect()
spark._jvm.java.lang.System.gc()&lt;/LI-CODE&gt;&lt;UL&gt;&lt;LI&gt;Batching the write every 100 tables to reduce write frequency&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Unfortunately, neither attempt prevents the memory usage from growing.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Questions:&lt;/STRONG&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Is there a reliable way to force JVM heap cleanup in this context?&lt;/LI&gt;&lt;LI&gt;Is this behavior a known limitation when using DESCRIBE DETAIL or DESCRIBE HISTORY iteratively at scale?&lt;/LI&gt;&lt;LI&gt;Are there more memory-efficient or scalable alternatives to gather metadata across many tables without requiring admin privileges?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Any advice or guidance is much appreciated! Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 07 Apr 2025 19:24:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/jvm-heap-leak-when-iterating-over-large-number-of-tables-using/m-p/114752#M44933</guid>
      <dc:creator>unnamedchunk</dc:creator>
      <dc:date>2025-04-07T19:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: JVM Heap Leak When Iterating Over Large Number of Tables Using DESCRIBE DETAIL</title>
      <link>https://community.databricks.com/t5/data-engineering/jvm-heap-leak-when-iterating-over-large-number-of-tables-using/m-p/119690#M45945</link>
      <description>&lt;P&gt;It's best to iterate over &lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/information-schema/tables" target="_self"&gt;information_schema's TABLES table&lt;/A&gt;&amp;nbsp;instead of listing yourself.&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 05:47:36 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/jvm-heap-leak-when-iterating-over-large-number-of-tables-using/m-p/119690#M45945</guid>
      <dc:creator>cgrant</dc:creator>
      <dc:date>2025-05-20T05:47:36Z</dc:date>
    </item>
  </channel>
</rss>

