用途
jmap 可以查看堆内对象的信息,生成 java 程序的 dump 文件,甚至可以查看 ClassLoader 的信息以及 finalizer 队列。
命令格式
jmap [option] <pid> # 连接到正在运行的进程
jmap [option] <executable <core> # 连接到核心文件
jmap [option] [server_id@]<remote server IP or hostname> # 连接到远程调试服务
- 1
- 2
- 3
参数说明:
- pid:目标进程的 PID
- executable:产生 core dump 的 java 可执行程序
- core:被打印信息的 core dump 文件
- server-id:可选的唯一 id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标识服务器
- remote server IP or hostname:远程调试服务器的 IP 地址或主机名
option 表示选项参数,常见的选项参数如下:
- no option:查看进程的内存映像信息
- heap:显示堆的详细信息
- histo[:live]:显示堆中对象的统计信息
- clstats:打印类加载器信息
- finalizerinfo:显示在 F-Queue 队列等待 Finalizer 线程执行 finalizer 方法的对象
- dump:<dump-options>:生成堆存储快照
- F:当 -dump 没有响应时,使用 -dump 或者 -histo 参数,在这个模式下,live 子参数无效
- help:帮助信息
- J<flag>:指定传递给运行 jmap 的 JVM 的参数
示例
下面我们来看几个使用 jmap 的例子:
注意,由于博主是在 docker 中执行相应的命令,故在新建并启动容器时要加上 --privileged=true --cap-add=SYS_PTRACE 配置,不然在 docker 中无法执行 jmap。
docker run --privileged=true --cap-add=SYS_PTRACE -d -p 8082:8082 mytest
- 1
一,no option
查看进程的内存映像信息。该命令打印共享对象映射,即打印目标虚拟机中加载的每个共享对象的起始地址、映射大小以及共享对象文件的路径全称。
root@973ba4cf6586:/# jmap 1
Attaching to process ID 1, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.111-b14
0x0000000000400000 6K /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
0x00007f99601cb000 75K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/libnio.so
0x00007f99609de000 94K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/libnet.so
0x00007f9960bf6000 38K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/libmanagement.so
0x00007f9975dd3000 34K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/libzip.so
0x00007f9975fdc000 46K /lib/x86_64-linux-gnu/libnss_files-2.19.so
0x00007f99761e8000 42K /lib/x86_64-linux-gnu/libnss_nis-2.19.so
0x00007f99763f3000 87K /lib/x86_64-linux-gnu/libnsl-2.19.so
0x00007f997660b000 30K /lib/x86_64-linux-gnu/libnss_compat-2.19.so
0x00007f9976813000 191K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/libjava.so
0x00007f9976a43000 74K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/libverify.so
0x00007f9976c56000 31K /lib/x86_64-linux-gnu/librt-2.19.so
0x00007f9976e5e000 87K /lib/x86_64-linux-gnu/libgcc_s.so.1
0x00007f9977074000 1026K /lib/x86_64-linux-gnu/libm-2.19.so
0x00007f9977375000 984K /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20
0x00007f9977680000 15805K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so
0x00007f9978613000 1697K /lib/x86_64-linux-gnu/libc-2.19.so
0x00007f99789be000 14K /lib/x86_64-linux-gnu/libdl-2.19.so
0x00007f9978bc2000 58K /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/jli/libjli.so
0x00007f9978dd1000 106K /lib/x86_64-linux-gnu/libz.so.1.2.8
0x00007f9978fec000 134K /lib/x86_64-linux-gnu/libpthread-2.19.so
0x00007f9979209000 137K /lib/x86_64-linux-gnu/ld-2.19.so
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
二,heap
显示堆的详细信息。使用该选项可以打印一个堆的摘要信息,包括使用的 GC 算法、堆配置信息和各区域内存使用信息。
root@7e2ee19d74ac:/# jmap -heap 1
Attaching to process ID 1, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.275-b01
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 482344960 (460.0MB)
NewSize = 10485760 (10.0MB)
MaxNewSize = 160759808 (153.3125MB)
OldSize = 20971520 (20.0MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 9502720 (9.0625MB)
used = 7132664 (6.802238464355469MB)
free = 2370056 (2.2602615356445312MB)
75.05918305495689% used
Eden Space:
capacity = 8454144 (8.0625MB)
used = 6412176 (6.1151275634765625MB)
free = 2041968 (1.9473724365234375MB)
75.84654342296511% used
From Space:
capacity = 1048576 (1.0MB)
used = 720488 (0.6871109008789062MB)
free = 328088 (0.31288909912109375MB)
68.71109008789062% used
To Space:
capacity = 1048576 (1.0MB)
used = 0 (0.0MB)
free = 1048576 (1.0MB)
0.0% used
tenured generation:
capacity = 20971520 (20.0MB)
used = 13845136 (13.203750610351562MB)
free = 7126384 (6.7962493896484375MB)
66.01875305175781% used
14836 interned Strings occupying 1393776 bytes.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
三,histo[:live]
显示堆中对象的统计信息,包括了每个 Java 类、对象数量、内存大小(单位:字节)、完全限定的类名。打印的虚拟机内部的类名称将会带有一个 * 前缀。如果指定了 live 子选项,则只计算活动的对象。
root@7e2ee19d74ac:/# jmap -histo:live 1
.
.
.
.
2481: 1 16 sun.reflect.generics.tree.ByteSignature
2482: 1 16 sun.reflect.generics.tree.CharSignature
2483: 1 16 sun.reflect.generics.tree.DoubleSignature
2484: 1 16 sun.reflect.generics.tree.FloatSignature
2485: 1 16 sun.reflect.generics.tree.IntSignature
2486: 1 16 sun.reflect.generics.tree.LongSignature
2487: 1 16 sun.reflect.generics.tree.ShortSignature
2488: 1 16 sun.reflect.generics.tree.VoidDescriptor
2489: 1 16 sun.security.provider.NativePRNG
2490: 1 16 sun.util.calendar.Gregorian
2491: 1 16 sun.util.locale.provider.AuxLocaleProviderAdapter$NullProvider
2492: 1 16 sun.util.locale.provider.CalendarDataUtility$CalendarWeekParameterGetter
2493: 1 16 sun.util.locale.provider.SPILocaleProviderAdapter
2494: 1 16 sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter
2495: 1 16 sun.util.resources.LocaleData
2496: 1 16 sun.util.resources.LocaleData$LocaleDataResourceBundleControl
Total 235864 12512632
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
四,clstats
打印类加载器信息。对于每个类加载器而言,它的名称、活跃度、地址、父类加载器、它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。
root@7e2ee19d74ac:/# jmap -clstats 1
Attaching to process ID 1, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.275-b01
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness..........................................................................liveness analysis may be inaccurate ...
class_loader classes bytes parent_loader alive? type
<bootstrap> 2026 3553710 null live <internal>
0x00000000ed382638 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed78ed78 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed299328 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed03af80 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed2999a0 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed33deb8 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed8bba80 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed7d3870 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ecdecad0 0 0 null live sun/misc/Launcher\(ExtClassLoader<span class="token annotation punctuation">@0x000000010000fd00</span>
<span class="token number">0x00000000ed78ebe8</span> <span class="token number">1</span> <span class="token number">880</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed2994b8</span> <span class="token number">1</span> <span class="token number">1473</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed23ecb0</span> <span class="token number">1</span> <span class="token number">1472</span> null dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed33e2a0</span> <span class="token number">1</span> <span class="token number">1471</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed3e7e28</span> <span class="token number">1</span> <span class="token number">1472</span> null dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed298db0</span> <span class="token number">1</span> <span class="token number">880</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed33dd28</span> <span class="token number">1</span> <span class="token number">1473</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed5095f0</span> <span class="token number">1</span> <span class="token number">1472</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed78e8d8</span> <span class="token number">1</span> <span class="token number">1472</span> null dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed299008</span> <span class="token number">1</span> <span class="token number">1473</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed33e110</span> <span class="token number">1</span> <span class="token number">1473</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed43d0e0</span> <span class="token number">1</span> <span class="token number">1471</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000ed299580</span> <span class="token number">1</span> <span class="token number">1473</span> <span class="token number">0x00000000ecdbd518</span> dead sun<span class="token operator">/</span>reflect<span class="token operator">/</span>DelegatingClassLoader<span class="token annotation punctuation">@0x000000010000a0a0</span>
<span class="token number">0x00000000eced1140</span> <span class="token number">0</span> <span class="token number">0</span> <span class="token number">0x00000000ecdbd8b0</span> live java<span class="token operator">/</span>util<span class="token operator">/</span>ResourceBundle\)RBClassLoader@0x00000001000b64b0
0x00000000ed299198 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed691fd8 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed33df80 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed43d270 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed8bafb8 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed382700 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed78ee40 1 1474 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed299710 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed03abb8 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed23c518 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed299a68 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ecdbd518 3662 5648946 0x00000000ecdbd8b0 live org/springframework/boot/loader/LaunchedURLClassLoader@0x0000000100060a10
0x00000000ed33ddf0 1 1474 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed748b30 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed7d3938 1 880 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed382470 1 1476 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed78ecb0 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed299260 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed0cddc0 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed15e2d8 1 1472 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed298e78 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed33dc60 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed7d3828 1 1472 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed78eb20 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed2993f0 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed2211f8 1 1472 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed3e7d60 1 1472 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed6f79b0 0 0 0x00000000ecdbd518 live org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedWebappClassLoader@0x00000001003cec70
0x00000000ed299648 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed298f40 1 1476 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ecdbd8b0 38 63472 0x00000000ecdecad0 live sun/misc/Launcher$AppClassLoader@0x000000010000f958
0x00000000ed33e1d8 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed43d1a8 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed4c6ea0 1 1505 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed3827c8 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed78ef08 1 880 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed2997d8 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed03a7f0 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed2990d0 1 1471 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed691f10 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed33e048 1 1473 0x00000000ecdbd518 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
0x00000000ed7d3a00 1 880 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0
total = 66 5786 9347389 N/A alive=6, dead=60 N/A
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
五,finalizerinfo
打印等待终结的对象信息。
root@7e2ee19d74ac:/# jmap -finalizerinfo 1
Attaching to process ID 1, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.275-b01
Number of objects pending for finalization: 0
- 1
- 2
- 3
- 4
- 5
- 6
可见等候回收的对象为0个。
六,dump:<dump-options>
生成堆存储快照 dump 文件,以 hprof 二进制格式存储 Java 堆到指定 filename 的文件中,如果你指定 live 子选项,堆中只有活动的对象会被存储。
执行这个命令,JVM 会将整个 heap 的信息 dump 写入到一个文件,如果 heap 很大的话,这个过程可能非常耗时,而且会在执行的过程中暂停应用。
root@7e2ee19d74ac:/# jmap -dump:format=b,file=heapdump.hprof 1
Dumping heap to /heapdump.hprof ...
Heap dump file created
- 1
- 2
- 3