Heap dump of a running JVM with jmap
You don’t have to wait for your JVM to die with an OutOfMemoryError to get the heap dump you get when you use the -XX:+HeapDumpOnOutOfMemoryError flag. Setting that flag is a good thing though – you get a dump after the heap is big and bloated just before the JVM dies making it easier to locate the problem are when analyzing the heap.
However, if you just want a dump of the current state of the heap – this is how you do it.
jps
Similar to ps. But shows only Java processes. Very nice! Use this to identify your target JVM pid. You can of course use the Unix ps command or the Windows tasklist command or the graphical TaskManager.
jmap -dump:file=heap.bin 924
That would dump the heap of the Java process with pid 924 into a file named heap.bin.

Simple enough, isn’t it? The tricky bit is to actually analyze a heap dump to extract information from all the data contained therein. I’ll write about analyzing heap dumps in a future post.
Got a quick tip with other useful ways to get heap dumps? Please leave a comment!
Tags: Java, JVM

I normally get take my heap dumps using jvisualvm.
With the MBeans plugin enabled you can go to MBeans tab and select: com.sun.management -> HotSpotDiagnostic.
Under the ‘operations’ tab there is a dumpHeap button. The two options it takes are:
1. filename. If you don’t specify a directory it will go into the processes current working directory.
2. Should the heap dump exclude anything eligible for garbage collection.
@Dan
True. JVisualVM is easier to use. But there can be problems with GUI access, firewalls and additional authentication such as Kerberos to deal with in “corporate” environments.
programmer…
[...]Heap dump of a running JVM with jmap « Onkar Joshi's blog[...]…
This is nice.
I used to get thread dumps from our Unix servers by remotely connecting through VisualVM from my windows desktop.
nice one