How to capture Java heap dump?
jmap tool under <JAVA_HOME>\bin folder is an effective option. It can be invoked using the command:
Here is how you should invoke jmap:
jmap -dump:format=b,file=<file-path> <pid>
where
pid: is the Java Process Id, whose heap dump should be captured
file-path: is the file path where heap dump will be written in to.
Example:
jmap -dump:format=b,file=/opt/tmp/heapdump.bin 37320
Note: It’s quite important to pass “l(fā)ive” option. If this option is passed, then only live objects in the memory are written into the heap dump file. If this option is not passed, all the objects, even the ones which are ready to be garbage collected are printed in the heap dump file. It will increase the heap dump file size significantly. It will also make the analysis tedious. To troubleshoot memory problems or optimize memory, just “l(fā)ive” option should suffice the need.
jmap
jmap print heap dumps into specified file location. This tool is packaged within JDK. It can be found in \bin folder.
[arthas@58205]$ heapdump arthas-output/dump.hprof
Dumping heap to arthas-output/dump.hprof ...
Heap dump file created
一搀崭、hprof文件簡(jiǎn)介
1、hprof文件是一種Java Heap Dump文件,可以通過(guò)jmap、Eclipse Memory Analyzer或者M(jìn)AT(Memory Analyzer Tool)等工具生成朋凉。
2屋匕、hprof文件記錄了Java應(yīng)用程序的運(yùn)行時(shí)快照信息命雀,包括Java虛擬機(jī)(JVM)在內(nèi)存中分配的各種對(duì)象翔始、對(duì)象類型倒脓、大小等信息撑螺。
3、hprof文件通常用于診斷Java應(yīng)用程序的內(nèi)存問(wèn)題崎弃,如內(nèi)存泄漏或過(guò)度分配等甘晤。
二、解析hprof文件
1饲做、通過(guò)jmap生成hprof文件:
jmap -dump:format=b,file=heap_dump.hprof
2安皱、通過(guò)Eclipse Memory Analyzer打開(kāi)hprof文件,并進(jìn)行分析:
(1)打開(kāi)Eclipse Memory Analyzer艇炎,選擇File -> Open Heap Dump -> 選擇hprof文件酌伊。