kill -3 to get java thread dump
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When dealing with Java applications, performance bottlenecks or deadlocks can sometimes occur, requiring developers to diagnose threads within a Java Virtual Machine (JVM). One effective way to achieve this is by generating a thread dump. This article provides an in-depth look at using the kill -3 command to generate a Java thread dump, offering technical explanations, examples, and best practices.
Understanding Java Thread Dump
A Java thread dump is a snapshot of all the threads that are active in a JVM at a given moment. It provides insights into the state of each thread, including:
- Thread ID
- Thread name
- Thread state (e.g., RUNNING, WAITING, BLOCKED)
- Lock information
- Stack trace
This diagnostic information is critical for troubleshooting performance issues like deadlocks, high CPU consumption, or application hangs.
The kill -3 Command
The kill -3 command is a UNIX signal used to request a thread dump in a Java application running on a UNIX-like OS. The command sends a SIGQUIT signal to the process, instructing the JVM to output a thread dump to the standard error stream or output file.
How kill -3 Works
- Identify the JVM Process ID (PID): Use system tools like
ps,jps, ortopto find the PID of the running Java application. - Execute
kill -3: Send aSIGQUITto the identified PID.
- Check Output Location: By default, the thread dump is printed to the console (stdout) or a log file specified by the JVM's standard error output. For applications running through a server, it may redirect to a file like
catalina.outfor Tomcat.
Example
Imagine you have a Java process with PID 12345. To generate a thread dump, execute:
Check the standard output or a configured log file to review the thread dump for analysis.
Interpreting Thread Dumps
Once generated, a thread dump can be analyzed to understand the JVM's state. Each thread is listed with details, forming a structure like this:
Key components:
- Thread Name: The name assigned to the thread, e.g., "MainThread."
- Thread ID (tid): A unique identifier within the JVM.
- Native Thread ID (nid): The operating system's thread identifier.
- Thread State: Current state of the thread (e.g., RUNNING, WAITING, or BLOCKED).
- Lock Information: Describes any locks the thread holds or is waiting for.
Advantages of Using kill -3
- Non-Invasive: It does not terminate the JVM or interfere with its normal operation.
- Immediate Output: Provides real-time thread activity, enabling rapid diagnosis.
- Accessibility: Available by default on most UNIX-like systems with no additional setup required.
Limitations
- Output Management: Depending on server configurations, locating the standard error output may require additional steps.
- Non-Windows Support: The
killcommand isn't natively available on Windows, as it uses a different signaling mechanism. Windows users might rely on tools likejstack.
Best Practices
- Redirect Output: Ensure standard error output points to easily accessible log files for persistent applications.
- Automated Tools: Consider using monitoring or profiling tools like VisualVM or JConsole for a more user-friendly experience.
- Minimize Production Interference: Prefer performing these actions during low-traffic periods in production environments.
Summary Table
Below is a comprehensive table summarizing key aspects of using kill -3.
| Aspect | Description / Explanation |
| Signal Used | SIGQUIT (signal number 3) |
| Typical Usage | kill -3 <PID> |
| Output Location | Standard error or configured log |
| Supported Environments | UNIX-like systems |
| Primary Use Case | Diagnosing JVM issues (e.g., deadlocks) |
| Non-Supported Environments | Windows (alternative methods apply) |
| JVM Impact | Minor (does not terminate or pause JVM) |
Conclusion
The kill -3 command is an essential tool for Java developers and system administrators, allowing for efficient and effective troubleshooting of JVM-based applications. When harnessed properly, it provides an immediate, in-depth view into an application's threading architecture, aiding in prompt resolution of performance problems.
Understanding how to generate, locate, and interpret thread dumps can significantly enhance one's ability to maintain robust Java applications, ensuring minimal downtime and optimal performance.
Related reading
- Kotlin Spring Boot ConfigurationProperties
- Lagom service consuming input from Kafka
- Lambda expression vs method reference
- Large scale machine learning - Python or Java?
- Kill detached screen session
- KMeans clustering - Value error n_samples1 should be n_cluster
- Likelihood of collision using most significant bits of a UUID in Java
- Limit a stream by a predicate

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.