I have a Java application that starts from a console but then spawns another Java process. I need to capture a thread dump and a heap dump of that child process.
On Unix, I could simply run kill -3 , but on Windows, the usual method is pressing Ctrl + Break in the console. The problem is that this only dumps the parent process, not the child process.
How can I get a Java heap dump for that specific child process on Windows?
Alright, so the easiest way to grab a Java heap dump from a running process on Windows is to use jcmd.
First, you need to find the Process ID (PID) of your Java process. Run this in Command Prompt:
jps -l
Once you have the PID, you can generate a heap dump like this:
jcmd <pid> GC.heap_dump heapdump.hprof
This will create a file named heapdump.hprof in your working directory.
You can then analyze it using tools like Eclipse MAT (Memory Analyzer) or VisualVM.
If jcmd doesn’t work for some reason, jmap is another option.
Just like before, find your PID using jps -l
, then run:
jmap -dump:format=b,file=heapdump.hprof <pid>
This will generate the heap dump file that you can analyze.
If you’re looking for a more visual approach, Java Mission Control (JMC) or VisualVM can help.
- Open JMC or VisualVM.
- Attach it to the running process.
- Navigate to the Heap Dump option and generate it with a single click.
This method is super helpful if you prefer a GUI-based approach instead of command-line tools.