Home / Databricks on Azure

Databricks on AzureRunbookAdvanced

Capturing JVM thread dumps and heap dumps on Azure Databricks for hung tasks and driver OOM

When a Databricks job hangs or the driver dies from OOM, logs rarely show why. Capture thread dumps, class histograms and heap dumps safely, then read them for the usual culprits.

Deepak Bhardwaj7 min read

Applies toAzure Databricks classic compute, Databricks Runtime 13.3 LTS and later, dedicated (single user) access mode for shell steps

A job has been stuck on the same stage for an hour, or the driver restarts with java.lang.OutOfMemoryError and the log only shows the last few lines. Retrying will not tell you why. A thread dump shows what each thread is waiting on, and a heap histogram or heap dump shows what is using memory. This runbook covers how to capture both on Azure Databricks without making the outage worse.

Important

Run the shell steps on a dedicated (single user) cluster that you own. Standard access mode and serverless limit shell access to the driver and do not allow JVM options, so on those, use the Spark UI steps only or reproduce the problem on a dedicated cluster.

Decide what to capture

Symptom Capture Cost
Stage or task stuck, no progress in Spark UI 3 thread dumps of the affected executor, 10–15 s apart Free, no pause
Driver unresponsive, notebook cells hang Driver thread dumps + GC.heap_info Free, no pause
Driver memory creeping up over hours GC.class_histogram every 15–30 min Short pause
Driver OOM you can reproduce Full heap dump (GC.heap_dump or on-OOM flag) Long stop-the-world pause, large file
Executor OOM Spark UI metrics, partition sizes; heap dump rarely worth it —

Begin with thread dumps. They are cheap and solve most hangs.

Step 1: thread dumps from the Spark UI

  1. Open the cluster, then Spark UI > Executors.
  2. Find the executor running the stuck task. Stages > the active stage > Tasks shows the executor ID and host.
  3. Click Thread Dump in that executor's row. The driver row has a Thread Dump link too.
  4. Save the page. Take two more dumps 10–15 seconds apart.

One dump is a snapshot. Three dumps tell you whether a thread is stuck in the same frame or still making progress. Search for the task thread. Executor task threads are named Executor task launch worker for task ....

Step 2: driver diagnostics from a notebook shell

You can get more from the driver with jcmd. Find the driver JVM first:

%sh
# The driver JVM's main class is the Databricks DriverDaemon
PID=$(pgrep -f DriverDaemon | head -1)
echo "driver pid: $PID"
which jcmd || ls "$JAVA_HOME/bin" | grep -E 'jcmd|jstack'

If jcmd is not on PATH, call it as $JAVA_HOME/bin/jcmd. Tool locations vary by runtime, so check the output above.

Take three thread dumps and a heap summary:

%sh
PID=$(pgrep -f DriverDaemon | head -1)
mkdir -p /local_disk0/tmp/diag
for i in 1 2 3; do
  jcmd "$PID" Thread.print -l > /local_disk0/tmp/diag/driver-threads-$(date +%H%M%S).txt
  sleep 10
done
jcmd "$PID" GC.heap_info
ls -lh /local_disk0/tmp/diag

To find what is using memory without a full dump, use a class histogram:

%sh
PID=$(pgrep -f DriverDaemon | head -1)
jcmd "$PID" GC.class_histogram | head -40

Warning

GC.class_histogram forces a full GC and pauses the driver. On a 100+ GB heap that can take tens of seconds, and running jobs may see heartbeat timeouts. Run it between workloads if you can.

Copy everything somewhere that survives cluster termination, such as a Unity Catalog volume:

%sh
cp /local_disk0/tmp/diag/* /Volumes/ops/diagnostics/dumps/

Step 3: full heap dump of the driver

Only take a heap dump when the class histogram does not explain the problem, for example when you need to know which object holds a large HashMap.

%sh
PID=$(pgrep -f DriverDaemon | head -1)
df -h /local_disk0          # need free space >= used heap
jcmd "$PID" GC.heap_dump /local_disk0/tmp/diag/driver.hprof
ls -lh /local_disk0/tmp/diag/driver.hprof

Caution

A heap dump stops the JVM until it finishes writing. The file is about as large as the live heap. Do not run it on a shared production cluster while other jobs are running. Heap dumps contain in-memory data, which can include credentials and customer records. Store them in a restricted volume and delete them when the investigation is done.

Capture automatically on OOM

If the OOM only happens inside a job, add JVM flags to a test cluster or job cluster so the JVM writes a dump when it fails:

spark.driver.extraJavaOptions -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/local_disk0/heapdumps

Create the directory with a cluster init script:

#!/bin/bash
mkdir -p /local_disk0/heapdumps

Warning

Before you set spark.driver.extraJavaOptions, check its current value under Spark UI > Environment, then append your flags to it rather than replacing it. On job clusters the local disk goes away when the run ends. Reproduce on an all-purpose dedicated cluster that stays up after the failure, then copy the .hprof to a volume.

Open the dump with Eclipse MAT or VisualVM on a workstation that has more RAM than the dump size. Look at the dominator tree and the "Leak Suspects" report.

Step 4: read the dumps

Pattern in the thread dump Likely cause Fix
Task thread in SocketInputStream.socketRead0 / NioSocketImpl.read for minutes External call (JDBC, REST, Key Vault) without a read timeout Set socket/query timeouts on the client, for example JDBC queryTimeout or socketTimeout
Many threads BLOCKED on the same monitor Lock contention in a shared client or singleton Create the client per partition (mapPartitions) or use a thread-safe pool
Driver main thread in ThreadUtils.awaitResult / DAGScheduler.runJob Driver is waiting on a Spark job. The problem is in the executors. Dump the executors running the active stage
Task thread busy in the same user frame across all 3 dumps Hot loop or a regex/UDF that is very slow on one partition Check task input size for skew, and replace the UDF with built-ins
Task thread in UnsafeExternalSorter.spill / ShuffleExternalSorter Heavy spill; it is progressing, just slowly Increase shuffle partitions or use bigger memory-optimized workers
Driver threads in ResultHandler / collect with heap near max collect() or toPandas() of a large result Write to a table and read it in pages, or aggregate first
Top entries in the class histogram Likely cause
[B, UnsafeRow, GenericRowWithSchema Collected rows held on the driver (collect, toPandas, broadcast building)
org.apache.spark.sql.execution.ui.SQLAppStatusListener-related classes, SparkPlanGraph Very many queries on a long-lived cluster. UI state grows
scala.collection.immutable.$colon$colon, TreeNode subclasses Huge query plans, often from loops that build withColumn chains or unions
Your own classes Caching or static maps in your library code

Driver GC pressure without OOM

The driver's stdout log (Compute > cluster > Driver logs) includes GC events on most runtimes. If full GCs come back to back and each frees little memory, the heap is too small for the work. You need a bigger driver or less data held on the driver. Extra executors will not help.

Python processes

If the driver JVM is idle but a Python cell is still running, the hang is in Python. You can install py-spy on a dedicated cluster and dump the Python stacks:

%sh
pip install py-spy
ps -eo pid,etime,rss,cmd | grep -i python | grep -v grep
py-spy dump --pid <python_pid>

py-spy needs ptrace permission, which some runtimes restrict. If it fails, add faulthandler.dump_traceback_later(600, repeat=True) from the standard library to the code to get periodic stacks in the driver log.

Prevent it next time

  • Set a timeout on every job and task (timeout_seconds) so a hang fails and pages someone instead of running all night.
  • Put read and connect timeouts on every JDBC and HTTP client used inside Spark tasks.
  • Keep collect() and toPandas() out of production code paths, or limit them explicitly.
  • Restart long-lived all-purpose clusters on a schedule if their driver memory keeps creeping up.
  • Keep a small dedicated "diagnostics" cluster policy that allows extraJavaOptions and shell access, so that you don't have to loosen production policies during an incident.