JVM Troubleshooting
How do you read a Java thread dump?
A thread dump is a snapshot of every thread in a JVM. Learn how to identify deadlocks, blocked threads, lock contention, and stuck requests.
What Is A Thread Dump?
A thread dump is a snapshot of every thread currently running inside a JVM.
Think of it as taking a photograph of the application at a particular moment in time.
When Would You Take One?
- Application appears hung
- Requests never finish
- High CPU usage
- Deadlock suspected
- Lock contention suspected
- Performance investigation
How Do You Generate A Thread Dump?
The JVM generates thread dumps.
The most common tools are:
jstack <pid>or:
kill -3 <pid>Many developers know:
kill -9which forcefully terminates a process.
But:
kill -3is special for Java applications.
Instead of terminating the JVM, it tells the JVM to dump information about all running threads into the logs.
The Four States You Will See Most Often
RUNNABLE
BLOCKED
WAITING
TIMED_WAITING
The Simplest Deadlock Example
Imagine a money transfer system with two accounts.
Thread A:
lock(account1)
lock(account2)
Thread B:
lock(account2)
lock(account1)If both threads execute simultaneously:
Thread A
Thread B
A Deadlock Program You Can Run Yourself
public class DeadlockDemo {
private static final Object account1 = new Object();
private static final Object account2 = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
synchronized (account1) {
sleep();
synchronized (account2) {
System.out.println("Transfer completed");
}
}
});
Thread t2 = new Thread(() -> {
synchronized (account2) {
sleep();
synchronized (account1) {
System.out.println("Transfer completed");
}
}
});
t1.start();
t2.start();
}
private static void sleep() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}Capturing The Thread Dump
Start the application.
Find the process id:
jpsThen:
jstack <pid>What The Thread Dump Might Show
Thread-0:
waiting to lock account2
locked account1
Thread-1:
waiting to lock account1
locked account2The JVM may even helpfully print:
Found one Java-level deadlockHow To Spot Lock Contention
Deadlocks are rare.
Lock contention is much more common.
BLOCKED
BLOCKED
BLOCKED
BLOCKED
BLOCKEDIf many threads are blocked waiting for the same lock, you may have a throughput bottleneck.
How To Spot A Hung Request
http-nio-8080-exec-17
at PaymentService.callBank()
at RestTemplate.exchange()
at ...The stack trace tells you exactly where the request is currently stuck.