持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第4天,点击查看活动详情
前言
jmap用来查看堆内存使用状况,一般结合jhat使用。但是这个命令好像并不常用,下面简单介绍一下这个命令的基本用法。
线上问题分析专题
深入理解jstack命令
深入理解jmap命令
深入理解jstat命令
这几篇主要介绍了一下JDK自带的几个分析工具,从线程,堆,垃圾回收几个方面如何去入手分析。 后面介绍一下线上问题分析神器Arthas的基本原理和使用。
Jhat命令简介
jhat 全称:Java Heap Analyse Tool(Java堆分析工具),jhat 也是 jdk 内置的工具之一。
功能:主要是用来分析java堆的命令,可以将堆中的对象以 html 的形式显示出来,包括对象的数量,大小等等,并支持对象查询语言(OQL)。
基本用法:
[app@dzopdev-010010195187 ~]$ jhat -h
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"
[app@dzopdev-010010195187 ~]$
-J jhat命令实际会启动一个jvm来执行,通过-J 可以指定一些启动参数
-stack false 关闭对象分配调用栈跟踪(tracking object allocation call stack)。 如果分配位置信息在堆转储中不可用,则必须将此标志设置为 false,默认值为 true.
-refs false:关闭对象引用跟踪(tracking of references to objects)。
-port : 设置 jhat HTTP server 的端口号,默认值 7000
-exclude : 指定对象查询时需要排除的数据成员列表文件(a file that lists data members that should be excluded from the reachable objects query)。
-baseline : 指定一个基准堆转储(baseline heap dump)。 在两个 heap dumps 中有相同 object ID 的对象会被标记为不是新的(marked as not being new),其他对象被标记为新的(new),在比较两个不同的堆转储时很有用。
-debug : 设置 debug 级别,0 表示不输出调试信息。 值越大则表示输出更详细的 debug 信息
-version 版本信息
-h|-help 查看帮助信息
jhat 使用
注意,jhat从JDK9的时候已经删除了(JEP 241: Remove the jhat Tool)。现在Oracle官方推荐的分析工具是Eclipse Memory Analyzer Tool (MAT) 和 VisualVM。
这边我简单介绍一下jhat的用法,jhat 主要流程,jhat解析dump文件,会在在本地启动一个web服务器,我们可以通过web页面来查看dump出来的数据。默认情况下web服务器的端口是7000。
1、 jmap命令导入dump文件
jmap -dump:live,file=demo.hprof <pid>
[app@dzopdev-010010195187 ~]$ jmap -dump:live,file=/home/app/dump.6770.hprof 6770
Dumping heap to /home/app/dump.6770.hprof ...
Heap dump file created
[app@dzopdev-010010195187 ~]$
2、jhat命令解析文件
[app@dzopdev-010010195187 ~]$ jhat dump.6770.hprof
Reading from dump.6770.hprof...
Dump file created Wed Jun 01 21:39:02 CST 2022
Snapshot read, resolving...
Resolving 535023 objects...
Chasing references, expect 107 dots...........................................................................................................
Eliminating duplicate references...........................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.
访问端口7000的网址
类的信息页面包含很多信息,包括父类,类加载器,签名,安全域,子类,实例,引用等详细信息。 对我们分析内存泄露和内存异常等情况非常有用。
OtherQueries
常用的功能
- 类直方图(类、实例数、总大小):http://10.10.195.187:7000/:7000/histo/
- 所有类实例统计:http://10.10.195.187:7000/showInstanceCounts/includePlatform/
- OQL查询:http://10.10.195.187:7000/oql/
- oql语法帮助:http://10.10.195.187:7000/oqlhelp/
注意:上面的IP是我本地的IP
总结
在JDK1.9 移除了 jhat。现在有很多更强大的内存观测和分析工具,我们平时遇到线上问题分析也是借助其他的一些客户端工具去做,原生的jhat命令工具,其实很不直观。