概述
Java Virtual Machine Heap Analysis Tool 虛擬機堆轉儲快照分析工具,用於分析heapdump文件,它會建立一個HTTP/HTML服務器,讓用戶可以在瀏覽器上查看分析結果。
Sun JDK提供了jhat(JVM Heap Analysis Tool)命令與jmap搭配使用,來分析jmap生成的堆轉儲快照。jhap會解析dump出來的文件,並在本地啟動一個web服務器,默認情況下web服務器的端口是7000。
在實際工作中,除非真的沒有別的工具可用,否則一般不會去直接使用jhat命令來分析demp文件,主要原因有二:
- 一般不會在部署應用程序的服務器上直接分析dump文件,即使可以這樣做,也會盡量將dump文件拷貝到其他機器上進行分析,因為分析工作是一個耗時且消耗硬件資源的過程,既然都要在其他機器上進行,就沒必要受到命令行工具的限制了;
- jhat的分析功能相對來說很簡陋,VisualVM以及專門分析dump文件的Eclipse Memory Analyzer、IBM HeapAnalyzer等工具,都能實現比jhat更強大更專業的分析功能。
注意,jhat從JDK9的時候已經刪除了(JEP 241: Remove the jhat Tool)。現在Oracle官方推薦的分析工具是Eclipse Memory Analyzer Tool (MAT) 和 VisualVM。
jhat 用法
[root@push ~]# jhat -help Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file> -J<flag> Pass <flag> directly to the runtime system. For example, -J-mx512m to use a maximum heap size of 512MB -stack false: Turn off tracking object allocation call stack. -refs false: Turn off tracking of references to objects -port <port>: Set the port for the HTTP server. Defaults to 7000 -exclude <file>: Specify a file that lists data members that should be excluded from the reachableFrom query. -baseline <file>: Specify a baseline object dump. Objects in both heap dumps with the same ID and same class will be marked as not being "new". -debug <int>: Set debug level. 0: No debug output 1: Debug hprof file parsing 2: Debug hprof file parsing, no server -version Report version number -h|-help Print this help and exit <file> The file to read For a dump file that contains multiple heap dumps, you may specify which dump in the file by appending "#<number>" to the file name, i.e. "foo.hprof#3". All boolean options default to "true"
option具體選項及作用如下:
- -J< flag >: 因為 jhat 命令實際上會啟動一個JVM來執行, 通過 -J 可以在啟動JVM時傳入一些啟動參數. 例如, -J-Xmx512m 則指定運行 jhat 的Java虛擬機使用的最大堆內存為 512 MB. 如果需要使用多個JVM啟動參數,則傳入多個 -Jxxxxxx
- -stack false|true: 關閉跟蹤對象分配調用堆棧。如果分配位置信息在堆轉儲中不可用. 則必須將此標志設置為 false. 默認值為 true.
- -refs false|true: 關閉對象引用跟蹤。默認情況下, 返回的指針是指向其他特定對象的對象,如反向鏈接或輸入引用(referrers or incoming references), 會統計/計算堆中的所有對象。
- -port port-number: 設置 jhat HTTP server 的端口號. 默認值 7000。
- -exclude exclude-file: 指定對象查詢時需要排除的數據成員列表文件。 例如, 如果文件列出了 java.lang.String.value , 那么當從某個特定對象 Object o 計算可達的對象列表時, 引用路徑涉及 java.lang.String.value 的都會被排除。
- -baseline exclude-file: 指定一個基准堆轉儲(baseline heap dump)。 在兩個 heap dumps 中有相同 object ID 的對象會被標記為不是新的(marked as not being new). 其他對象被標記為新的(new). 在比較兩個不同的堆轉儲時很有用。
- -debug int: 設置 debug 級別. 0 表示不輸出調試信息。 值越大則表示輸出更詳細的 debug 信息。
- -version: 啟動后只顯示版本信息就退出。
示例一:no option
命令:jhat -port 9998 /tmp/heapdump.hprof
描述:生成堆轉儲快照dump文件。
root@ubuntu:/# jhat -port 9998 /tmp/heapdump.hprof Reading from /tmp/heapdump.hprof... Dump file created Tue Jan 28 17:46:14 CST 2014 Snapshot read, resolving... Resolving 132207 objects... Chasing references, expect 26 dots.......................... Eliminating duplicate references.......................... Snapshot resolved. Started HTTP server on port 9998 Server is ready.
注意如果Dump文件太大,可能需要加上-J-Xmx512m這種參數指定最大堆內存,即 jhat -J-Xmx512m -port 9998 /tmp/heapdump.hprof。
然后就可以在瀏覽器中輸入主機地址:9998查看了:
上面紅線框出來的部分大家可以自己去摸索下,最后一項支持OQL(對象查詢語言)。