How to get a thread and heap dump of a Java process on Windows that's not running in a console
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Analyzing Java applications in production often involves taking thread and heap dumps to diagnose issues such as memory leaks, deadlocks, or performance bottlenecks. For Java processes not running in a console on Windows, this task might appear daunting but can be executed efficiently using tools provided with the Java Development Kit (JDK) and third-party utilities.
Tools and Commands
- VisualVM: Part of the JDK, VisualVM is a robust graphical tool that allows for easy monitoring and troubleshooting of Java applications. It lets you take thread and heap dumps with a simple click.
- JConsole: Another tool included with the JDK, JConsole can connect to a running Java process and provide diagnostic capabilities including heap dump generation.
- Command Line Tools:
jps: Identifies Java process IDs, essential for other commands.jstack: For generating thread dumps.jmap: For generating heap dumps.
Detailed Procedures
Using VisualVM:
- Installation: If not already available, download VisualVM from its official site and install it.
- Running VisualVM:
- Open VisualVM from the Start Menu or its installation directory.
- The tool automatically detects running Java processes on the local machine within its GUI.
- Taking Dumps:
- Select the Java process you want to diagnose.
- Navigate to the "Monitor" tab, click "Thread Dump" for a thread dump.
- Go to the "Sampler" tab, and click on "Heap Dump" to create a heap dump.
Using JConsole:
- Launch JConsole: Type
jconsolein the command prompt. - Connect to a Java Process:
- A window pops up listing local processes. Select the Java process you want to inspect and click "Connect".
- Generate Dumps:
- For a heap dump, navigate to the "MBeans" tab, open
com.sun.management, selectHotSpotDiagnosticMXBean, and invokedumpHeapoperation specifying a path for the dump file.
Command Line Techniques:
- Identifying the Process ID:
- Open a command prompt and type
jps -lto list the Java processes and their IDs.
- Generating a Thread Dump:
- Execute
jstack -l <PID>(where<PID>is the process ID). This will print the thread dump to the console, which you can redirect to a file using> filename.txt.
- Creating a Heap Dump:
- Type
jmap -dump:live,format=b,file=myheapdump.hprof <PID>. - The generated file
myheapdump.hprofcan be analyzed with tools like Eclipse Memory Analyzer.
When to Take Thread and Heap Dumps
Analyzing Java applications should be done judiciously to avoid affecting performance. Typically, dumps are taken when there are evident performance issues like slow application response or when memory usage trends abnormally.
Summary Table
| Tool/Command | Usage | Type of Dump | Output |
| VisualVM | GUI-based, easy | Thread and Heap | Interactive Environment |
| JConsole | GUI-based, versatile | Heap | Interactive Environment |
| jps/jstack | CLI, for thread diagnostics | Thread | File/Console Output |
| jmap | CLI, for memory diagnostics | Heap | File Output |
| jmap (options) | Advanced configuration | Custom | File Output |
Conclusion
Thread and heap dumps are pivotal for diagnosing issues in a Java application. Although the process may seem complex, utilizing the correct tools like VisualVM, JConsole, and command-line utilities simplify it. It's essential to familiarize oneself with these tools before encountering issues that require such analysis, ensuring preparedness for effective troubleshooting.
Related reading
- How to get access of individual trees of a xgboost model in python /R
- How to get all 24 rotations of a 3-dimensional array?
- How to get all algebraic associative operations on a finite set by efficient algorithm?
- How to get all enum values as an array
- How to get active user's UserDetails
- How to Get All Endpoints List After Startup, Spring Boot
- How to get around the memory leak in the .NET Webbrowser control?
- How to get back Kafka producer and consumer configuration (Java API)?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.