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.
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