JVM——jhat命令

 

概述

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 AnalyzerIBM 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(对象查询语言)。

 

posted on 2021-05-20 10:36  曹伟雄  阅读(2873)  评论(0编辑  收藏  举报

导航