首先用Android studio 生成hprof文件,比如文件名叫:xxx.hprof
然后用hprof-conv xxx.hprof memory-converted.hprof
因为MAT只支持java版的hprof,as生成的hprof只能android版本的hprof,直接导入MAT会报错。
然后通过mat打开这个memory-converted.hprof
然后open a Heap Dump-> Leak Suspects Report
通过分析可以看到Bitmap 占用的内存比较多 点开detail->Suspect Objects by Class
first 10 of 304objects 点开对象列表
然后找最大的对象右键Path to gc Roots->Exclude Weak References 然后就可以看到对象的引用了。
我做了一个自动转hprof的脚本,只要把这个做成app就可以把xxx.hprof 拖到这个app上,就能生成convert的hprof了
javascript
on open droppedItems
repeat with oneItem in droppedItems
set inputPath to POSIX path of oneItem
-- 输出到原文件同目录,文件名自动加 -converted
do shell script "
input_file=" & quoted form of inputPath & "
dir=$(dirname \"$input_file\")
base=$(basename \"$input_file\" .hprof)
output_file=\"$dir/${base}-converted.hprof\"
/opt/homebrew/bin/hprof-conv \"$input_file\" \"$output_file\"
"
end repeat
display dialog "转换完成" buttons {"确定"} default button "确定"
end open
然后可以通过打开Top Components->点开内存最大的比如<system class loader> (80%)->Top Consumer->Biggest Objects 根据Retained Heap 排序,找大的对象进行分析