1 概述
命令jmap是一个多功能的命令。它可以生成 java 程序的 dump 文件, 也可以查看堆内对象示例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。
[webuser@-TESTENV interaction-like]$ jmap -help
Usage:
jmap [option] <pid>
(to connect to running process)
jmap [option] <executable <core>
(to connect to a core file)
jmap [option] [server_id@]<remote server IP or hostname>
(to connect to remote debug server)
where <option> is one of:
<none> to print same info as Solaris pmap
-heap to print java heap summary
-histo[:live] to print histogram of java object heap; if the "live"
suboption is specified, only count live objects
-clstats to print class loader statistics
-finalizerinfo to print information on objects awaiting finalization
-dump:<dump-options> to dump java heap in hprof binary format
dump-options:
live dump only live objects; if not specified,
all objects in the heap are dumped.
format=b binary format
file=<file> dump heap to <file>
Example: jmap -dump:live,format=b,file=heap.bin <pid>
-F force. Use with -dump:<dump-options> <pid> or -histo
to force a heap dump or histogram when <pid> does not
respond. The "live" suboption is not supported
in this mode.
-h | -help to print this help message
-J<flag> to pass <flag> directly to the runtime system
参数:
- pid: 需要打印配置信息的进程ID。
- heap: 显示Java堆详细信息
- histo[:live]: 显示堆中对象的统计信息
- clstats:打印类加载器信息
- finalizerinfo: 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
- dump:
: 生成堆转储快照 - F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
- help:打印帮助信息
- J
: 指定传递给运行jmap的JVM的参数
2 常用命令
2.1 jmap -heap pid
[webuser@CSDN-TESTENV interaction-like]$ jmap -heap 7585
Attaching to process ID 7585, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12
using thread-local object allocation.
Parallel GC with 2 thread(s) //GC 方式
Heap Configuration: //堆内存初始化配置
MinHeapFreeRatio = 0 //对应jvm启动参数-XX:MinHeapFreeRatio设置JVM堆最小空闲比率(default 40)
MaxHeapFreeRatio = 100 //对应jvm启动参数 -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率(default 70)
MaxHeapSize = 2082471936 (1986.0MB) //对应jvm启动参数-XX:MaxHeapSize=设置JVM堆的最大大小
NewSize = 1310720 (1.25MB)//对应jvm启动参数-XX:NewSize=设置JVM堆的‘新生代’的默认大小
MaxNewSize = 17592186044415 MB//对应jvm启动参数-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
OldSize = 5439488 (5.1875MB)//对应jvm启动参数-XX:OldSize=<value>:设置JVM堆的‘老生代’的大小
NewRatio = 2 //对应jvm启动参数-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
SurvivorRatio = 8 //对应jvm启动参数-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值
PermSize = 21757952 (20.75MB) //对应jvm启动参数-XX:PermSize=<value>:设置JVM堆的‘永生代’的初始大小
MaxPermSize = 85983232 (82.0MB)//对应jvm启动参数-XX:MaxPermSize=<value>:设置JVM堆的‘永生代’的最大大小
G1HeapRegionSize = 0 (0.0MB)
Heap Usage: //堆内存使用情况
PS Young Generation
Eden Space:
capacity = 110624768 (105.5MB)
used = 92517392 (88.23146057128906MB)
free = 18107376 (17.268539428710938MB)
83.63171618131665% used
From Space:
capacity = 110624768 (105.5MB)
used = 819200 (0.78125MB)
free = 109805568 (104.71875MB)
0.740521327014218% used
To Space:
capacity = 110624768 (105.5MB)
used = 0 (0.0MB)
free = 110624768 (105.5MB)
0.0% used
PS Old Generation
capacity = 337641472 (322.0MB)
used = 33530208 (31.976898193359375MB)
free = 304111264 (290.0231018066406MB)
9.930713724645768% used
25039 interned Strings occupying 2681056 bytes.
2.2 jmap -histo:live pid
显示堆中对象的统计信息,其中包括每个Java类、对象数量、内存大小(单位:字节)、完全限定的类名。打印的虚拟机内部的类名称将会带有一个’*’前缀。如果指定了live子选项,则只计算活动的对象。
[webuser@CSDN-TESTENV root]$ jmap -histo:live 7585
num #instances #bytes class name
----------------------------------------------
1: 74899 8341672 [C
2: 68280 2184960 java.util.concurrent.ConcurrentHashMap$Node
3: 17683 1950672 java.lang.Class
4: 17245 1842640 [Ljava.lang.Object;
5: 4289 1819152 [B
6: 20618 1814384 java.lang.reflect.Method
7: 74269 1782456 java.lang.String
8: 9767 1164856 [I
9: 46643 746288 java.lang.Object
10: 298 712544 [Ljava.util.concurrent.ConcurrentHashMap$Node;
11: 6865 598600 [Ljava.util.HashMap$Node;
12: 14097 563880 java.util.LinkedHashMap$Entry
13: 192 497400 [J
14: 7552 422912 java.util.LinkedHashMap
15: 12501 400032 java.util.HashMap$Node
16: 8184 392832 org.aspectj.weaver.reflect.ShadowMatchImpl
17: 14789 341704 [Ljava.lang.Class;
18: 8184 261888 org.aspectj.weaver.patterns.ExposedState
19: 4154 199392 org.springframework.core.ResolvableType
20: 7667 184008 java.util.ArrayList
21: 5651 180832 java.lang.ref.WeakReference
22: 4464 178560 java.lang.ref.Finalizer
23: 2633 177336 [Ljava.lang.String;
....
2.3 jmap -finalizerinfo pid
打印等待回收的对象信息
[webuser@-TESTENV root]$ jmap -finalizerinfo 7585
Attaching to process ID 7585, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12
Number of objects pending for finalization: 0
# 说明当前F-QUEUE队列中并没有等待Fializer线程执行finalizer方法的对象。
2.4 jmap -dump:
描述:生成堆转储快照dump文件。以hprof二进制格式转储Java堆到指定filename的文件中。live子选项是可选的。如果指定了live子选项,堆中只有活动的对象会被转储。想要浏览heap dump,可以用MAT、VisualVM或jhat等工具查看。
这个命令执行,JVM会将整个heap的信息dump写入到一个文件,heap如果比较大的话,就会导致这个过程比较耗时,并且执行的过程中为了保证dump的信息是可靠的,所以会暂停应用, 线上系统慎用。
[webuser@CSDN-TESTENV interaction-like]$ jmap -dump:live,format=b,file=heapDump 7585
Dumping heap to /data/logs/interaction-like/heapDump ...
Heap dump file created
[webuser@CSDN-TESTENV interaction-like]$ jhat -port 5000 heapDump //通过浏览器访问,有时你dump出来的堆很大,在启动时会报堆空间不足的错误,需要指定大小
Reading from heapDump...
Dump file created Thu Apr 22 15:35:50 CST 2021
Snapshot read, resolving...
Resolving 614075 objects...
Chasing references, expect 122 dots..........................................................................................................................
Eliminating duplicate references..........................................................................................................................
Snapshot resolved.
Started HTTP server on port 5000
Server is ready.