转自:http://blog.csdn.net/linghunhong/article/details/6451220
 
如何获取java heap dump
2010-06-25 12:43
C:/Program Files/Java/jdk1.6.0_20/bin>jmap
Usage:
jmap -histo <pid>
(to connect to running process and print histogram of java object heap
jmap -dump:<dump-options> <pid>
(to connect to running process and dump java heap)

dump-options:
format=b     binary default
file=<file> dump heap to <file>

Example:       jmap -dump:format=b,file=heap.bin <pid>


或者,运行jconsole, 选择 MBeans -> com.sun.management -> HotSpotDiagnostic -> Operations -> dumpHeap 在参数p0中填写保存dump文件的路径,如果要使用Eclipse Memory Analyzer来分析则文件的后缀要为hprof。点击dumpHeap按钮生成dump文件。


或者在Eclipse Memory Analyzer中直接生成
如何获取java heap dump _如何


或者,设置jvm让其在out of memory的时候自动生成dump文件: -XX:+HeapDumpOnOutOfMemoryError 或者-XX:+HeapDumpOnCtrlBreak,更多设置见http://wiki.eclipse.org/index.php/MemoryAnalyzer#Getting_a_Heap_Dump


附带一句,dump文件可以使用Eclipse Memory Analyzer
http://dev.eclipse.org/blogs/memoryanalyzer/2008/04/21/immortal-objects-or-how-to-find-memory-leaks/
http://wiki.eclipse.org/index.php/MemoryAnalyzer#Finding_Memory_Leaks

Finding Memory Leaks

Start by running the leak report to automatically check for memory leaks.
This blog details How to Find a Leaking Workbench Window.
The Memory Analyzer grew up at SAP. Back then, Krum blogged about Finding Memory Leaks with SAP Memory Analyzer. The content is still relevant!

Analyzing Java Collection Usage

Check out Krum's blog about Analyzing Java Collections Usage with Memory Analyzer. Also, Memory for Nothing looks unused collections and the memory kept alive.

Perm Space Issus

A good starting point is the blog The Unknown Generation: Perm by Andreas.
Also, Vedran has blogged some hints how to address Perm Space Issues.