使用MAT命令行工具生成堆dump分析文件


寫作目標

Java程序運行過程中,難免會出現 OOM,往往是在 JVM 啟動參數中添加出現 OOM 時輸出堆 dump(又名:堆轉儲、堆快照)的參數,就在昨天下午我司生產環境出現 OOM,生成的堆 dump 有 16 個G,壓縮后傳回本地,想分析時犯了難——本地開發機 16G 內存,用 MAT 打不開!

通過查詢相關文檔,發現可以使用 MAT 命令行腳本生成可供 MAT 圖形化界面展示的文件,也就是 用 Linux 服務器進行分析大堆/超大堆 dump 文件,輸出分析報告,通過瀏覽器直接查看!

運行環境

  • CentOS 7.4
  • MemoryAnalyzer-1.10.0.20200225-linux.gtk.x86_64.zip
  • 16G堆dump
  • 開發機Win10,16G內存

MAT 下載地址 http://www.eclipse.org/mat/downloads.php

操作步驟

1 上傳並解壓 MAT 到服務器

上傳 MAT 壓縮包到服務器上

解壓縮到Home目錄

unzip -q Memory*.zip -d /home/hellxz

2 上傳堆 dump 文件到服務器

生產環境打堆 dump 及壓縮,僅作演示,我這邊是自動打出來的。

# 生成堆 dump
jmap -dump:format=b,file=heap_dump_20210128-1505.hprof 10532
# 壓縮堆 dump
tar zcf /data/heap_dump_20210128-1505.tar.gz \ 
        heap_dump_20210128-1505.hprof

tar.gz壓縮16G的堆dump僅占 4.77G,這里上傳到了 MAT 分析服務器 /home/hellxz/performance 目錄下

cd performance
tar zxf heap_dump_20210128-1505.tar.gz

3 調整 MAT 配置文件

cd /home/hellxz/mat
ls
vim MemoryAnalyzer.ini

-startup
plugins/org.eclipse.equinox.launcher_1.5.0.v20180512-1130.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.700.v20180518-1200
-vmargs
-Xms30g
-Xmx58g
-XX:+UseG1GC
-DhprofStrictnessWarning=true
  • -vmargs 下邊是指定JVM參數的地方,默認的 -Xmx1024m 太小了,另外,我這個堆實在太大,試了好多遍,發現58g左右才不會溢出……如果在后續操作中出現報錯為 heap space 而停止分析,說明堆內存小了,改它!
  • -DhprofStrictnessWarning=true 參數最好加上,防止因為某些不必要的提示停止分析

4 執行 MAT 分析堆dump

cd /home/hellxz/mat
#執行 MAT 命令行腳本
./ParseHeapDump.sh \
    /home/hellxz/performance/heap_dump_20210128-1505.hprof  \
    org.eclipse.mat.api:suspects \
    org.eclipse.mat.api:overview \
    org.eclipse.mat.api:top_components
  • MAT 會將文件分析文件與臨時文件輸出到堆 dump 目錄下,臨時文件中包含 .index 結尾的索引文件,首次分析會建立索引速度較慢,后續再執行其他任務就很快了
  • suspects / overview / top_componets 這幾個 API 與 MAT 界面上顯示的區域是一致的,對應這個命令中是 3 個任務,即生成名為 xxx_Leak_Suspects.zip / xxx_System_Overview.zip / xxx_Top_Components.zip 的分析報告,可選擇分析三者任幾個任務
  • 如執行出現錯誤,請去文末查找錯誤解決方法

MAT 分析時間較長,等待一段時間后,能得到三個 zip 壓縮包
xxx_Leak_Suspects.zip / xxx_System_Overview.zip / xxx_Top_Components.zipxxx 會被替換為堆 dump 的名稱

5 使用瀏覽器查看報告

xxx_Leak_Suspects.zip / xxx_System_Overview.zip / xxx_Top_Components.zip 三個壓縮包下載到本地,分別解壓,雙擊 index.html 使用瀏覽器查看即可。
如圖,打開了 Leak_Suspects 解壓的 index.html

真不是我屏幕黃,是校色工具搞的……

Q&A

1 提示X11轉發打開窗口

取消打開窗口則報錯: java.lang.InternalError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.

解決辦法:

echo "export DISPLAY=:1" /etc/profile
source /etc/profile

對於安裝了桌面的臨時處理辦法,用完后修改回來,如果沒裝桌面就不用改回來了

2 提示 heap space,停止分析

設置mat目錄下的 MemoryAnalyzer.ini,在 -vmargs 下調整JVM最大堆大小

3 提示 Unable to initialize GTK+

ParseHeapDump.sh 執行時調用了GTK的組件,當前OS環境不滿足就會報錯,解決方式是繞過GTK組件:

1)獲取啟動包路徑

cd mat
ls plugins/org.eclipse.equinox.launcher*.jar

復制紅字部分,一會改腳本需要用

2)修改 ParseHeapDump.sh

cd mat
vim ParseHeapDump.sh

注釋第11行,添加新的啟動命令

java -Xmx16g -DhprofStrictnessWarning=true -jar \
     "$(dirname -- "$0")"/plugins/org.eclipse.equinox.launcher_1.5.0.v20180512-1130.jar \
-consolelog -application org.eclipse.mat.api.parse "$@"

  • -jar 前為自定義JVM參數
  • plugins/org.eclipse.equinox.launcher_1.5.0.v20180512-1130.jar 請替換為第一步中獲取的jar包名

未經許可禁止轉載,如需轉載請注明出處!https://www.cnblogs.com/hellxz/p/use_mat_linux_command_line_generate_reports.html
本文同步於本人CSDN


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM