java性能分析--jhat

360 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第12天,点击查看活动详情

前一章节介绍了jmap相关的信息,其中使用-dump生成的堆转储文件,我们还没有看它是干什么的,本章我们使用jhat命令,来分析下,堆转储文件到底有什么内容,能够帮助我们发现什么问题。

jhat命令

jhat命令主要用来分析由jmap导出的堆转储文件。并非实时使用的工具,所以也可以称它为事后使用工具。

我们来看下它的帮助文档:

[root@hecs-402944 ~]# 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"

可以看到其使用方式如下所示:

jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>

其中每个参数的含义如下:

  • -J<flag> 用于指定读取堆转储文件时,可以使用的最大堆空间。
  • -stack false 关闭跟踪对象分调用配栈。默认true
  • -refs false 关闭跟踪对象引用,默认true
  • -port <port> 设置一个http服务的端口,默认是7000.
  • -exclude <file> 指定一个文件,列出需要排除的数据。
  • -baseline <file> 指定一个基准的堆转储,如果对象在多个堆栈中具有相同id和class将不被标记位new出来的对象。
  • -debug <int> 设置debug级别
  • -version 输出版本号
  • <file> 被读取的文件

通过前面对于其参数的分析,我们能够知道,除了file这个参数,其余的可以默认,不用设置,我们直接使用上一章节通过jmap导出的thread-21203.log文件进行测试:

[root@hecs-402944 ~]# jhat thread-21203.log 
Reading from thread-21203.log...
Dump file created Mon Jun 06 10:45:15 CST 2022
Snapshot read, resolving...
Resolving 334745 objects...
Chasing references, expect 66 dots..................................................................
Eliminating duplicate references..................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

如上所示,我们发现服务启动成功了,端口是7000,需要注意你的防火墙是否开放了这个7000端口,如果是与服务器,请设置安全组开放7000!

我们在浏览器访问看结果:

image.png

可以找到自己的业务代码块:

image.png

它提供了全部的类,实例,和堆空间的柱状图等信息,可以在如下位置找到:

image.png

在上图中的最后一项,还提供了查询OQL:

image.png

如上图所示,查询字符串长度超过500的类。

总结

关于其他的内容就不多做介绍了,当前我使用jdk8还可以使用此功能,后续的新版本jdk将会对此功能删除,使用jdk8之后的同学们不需要过多了解,知道即可。