How To Obtain Heap Dump With JDK 5.0
By Tomas Hurka on Jul 19, 2007
There are various ways how to obtain heap dump with JDK 5.0.
- You can request that a heap dump will be created when an OutOfMemoryError is first thrown by using -XX:+HeapDumpOnOutOfMemoryError HotSpot VM option. Note that NetBeans Profiler automagically uses this option if you run your application under profiler. This means that heap dump is done and opened in HeapWalker if you encounter an OutOfMemoryError while doing profiling or monitoring.
- You can use HPROF: Heap and CPU Profiling Agent. A complete dump of the current live objects in the heap can be obtained with:
java -agentlib:hprof=heap=dump,format=b -jar application
This will automatically dump heap when java application is terminated. You can also force heap dump by sending QUIT signal to java process with
kill -QUIT pidcommand.
This option causes the greatest amount of memory to be used because it stores details on every object allocated, it can also impact the application performance due to the data gathering.
- jmap can also create heap dump directly from running java application with command:
jmap -heap:format=b pid
however jmap -heap:format=b in JDK 5.0 is more suited for recovering a heap dump from a core dump file rather than taking the heap dump of a running java application. jmap can observe the heap in an inconsistent state and hence the dump file is not always useful. It is also slow because it is reading the target process though /proc. And finally it is available only on Solaris, Mac OS X and Linux. So jmap should be avoided for taking heap dumps of running java applications on JDK 5.0. Note that for JDK 6 this has been fixed and jmap can produce consistent heap dump of running JDK 6 applications and is also available on Windows.
It looks like there is no good way to obtain a heap dump at runtime with JDK 5.0. Both jmap and hprof native agent library is not suitable to get heap dump, but fortunately NetBeans Profiler has Dump Heap action which can be invoked either during profiling session from Profile menu or during memory profiling as a secondary action of Take Snapshot button in Profiler Control Panel. It will create consistent heap dump at runtime even on JDK 5.0. The only condition is to use latest JDK 5.0 update 12.