mac平台下怎么分析java堆hprof文件

使用 jdk 自带的 jhat 命令

如何打印堆栈信息到hprof文件中
运行程序增加VM参数(如何增加VM执行参数,可以查看之前文章):

-XX:+HeapDumpOnOutOfMemoryError # 会打印出现内存溢出异常时,Dump出当前内存堆转存快找以便分析

步骤1:
在命令行界面,执行以下命令,其中java_pid73912.hprof为文件名称

jhat java_pid73912.hprof 

会输出

Reading from java_pid73912.hprof...
Dump file created Thu Apr 08 22:18:08 CST 2021
Snapshot read, resolving...
Resolving 818890 objects...
Chasing references, expect 163 dots...................................................................................................................................................................
Eliminating duplicate references...................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

当看出Server is ready. 之后,可以通过 ip + port 地址浏览器访问,我这里是本地,所以是 localhost:7000

在这里插入图片描述

点击Show instance counts for all classes (including platform)会展示包含平台的类的的信息

在这里插入图片描述

点击Show instance counts for all classes (excluding platform)会展示不包含平台的所有类的信息

在这里插入图片描述

可以看到我这里占用空间最多的类是 class com.geniu.book.deepinJVM.chapter2.TestHeapOOM$OOMObject有 80 多万个实例。

其他建议方案:

1)使用idea插件 JProfiler

2)minidump_stackwalk 工具,可以读取 dump 文件

posted @ 2021-04-11 10:14  六七十三  阅读(2643)  评论(0编辑  收藏  举报