Wednesday, April 3, 2013

jmap & Windows7

In spite of using Java for several decades now, I had never used jmap. jmap is a JDK tool used for dumping the heap memory details of a process. jmap worked with no problems at all on the Ubuntu machine which had OpenJDK installed.

However, as soon as I tried using the same commands on my Windows 7 machine, the command just didn't do anything. I did search online and found no references on why it doesn't work the way it is supposed to on Windows. Didn't spend too much time finding what the root cause was either. The workaround is sufficient for me. :)

So, lets see how to get this jmap working on Windows.

Step 1:
Open a command window and start any Java program you have. Make sure it runs for a while so you can use this process ID to get the heap dump.

Step 2:
Run the jps command to get the process ID's. jps is the Java Virtual Machine Process Status Tool.

Step 3:
Now that you have the process ID's for the applications running, start the jmap command on another command prompt. This is where things get interesting.

jmap should have worked here, since it is in the path. However, it doesn't dump the heap and keeps displaying the usage instructions.
I tried several options, none seemed to work.

Step 4:
At this point, I decided to run the jmap command from the bin folder of the JDK to see if anything changes.

And like a charm, jmap dumped the contents of the heap.

Step 5:
Once you have the contents of the heap, use jhat the Java Heap Analysis Tool to view and browse through the heap dump file.

Below are links for the various JDK Tools:

1. jmap
2. jps
3. jhat