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文件簡介
1、hprof文件是一種Java Heap Dump文件,可以通過jmap、Eclipse Memory Analyzer或者MAT(Memory Analyzer Tool)等工具生成。
2、hprof文件記錄了Java應用程序的運行時快照信息,包括Java虛擬機(JVM)在內(nèi)存中分配的各種對象、對象類型、大小等信息。
3、hprof文件通常用于診斷Java應用程序的內(nèi)存問題,如內(nèi)存泄漏或過度分配等。
二、解析hprof文件
1、通過jmap生成hprof文件:
jmap -dump:format=b,file=heap_dump.hprof
2、通過Eclipse Memory Analyzer打開hprof文件,并進行分析:
(1)打開Eclipse Memory Analyzer,選擇File -> Open Heap Dump -> 選擇hprof文件。