持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第11天,点击查看活动详情
本篇继续学习java性能分析,jmap相关的命令和使用方式。
堆转储文件简介
堆转储是指jvm当中,某个时间点程序内存的快照,其中包含内存中的对象,对象的值和大小,以及对象所引用的其他对象。堆转储文件是二进制的格式,所以我们无法直接的读取它。它通常被用来解决和分析内存溢出等问题。
jmap 获取堆转储文件的工具
我们可以通过jmap去获取堆转储文件。当然jmap的功能可不止于此,我们下面来学习一下。
通过help查看jmap的使用方式:
[root@hecs-402944 ~]# jmap -h
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
如上所示提供了三种使用方式,以及提供[option]的具体解释。下面简单介绍几种常用的:
jmap [option] <pid>连接正在运行的线程jmap [option] <executable <core>连接可执行的核心 本章不讲解jmap [option] [server_id@]<remote server IP or hostname>连接远程debug的服务 本章不讲解
jmap [option] <pid>
获取服务的id:
[root@hecs-402944 ~]# jps
21203 jar
31309 Jps
按照给定的[option],分别查看其结果:
-
jmap -heap 21203
其中分别打印了:
- 并发GC占用的线程数
- 当前jvm的堆内存的配置
- 堆使用情况,包括年轻代和老年代的内存使用空闲情况。
[root@hecs-402944 ~]# jmap -heap 21203 Attaching to process ID 21203, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.332-b09 using thread-local object allocation. Parallel GC with 2 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 1073741824 (1024.0MB) NewSize = 357564416 (341.0MB) MaxNewSize = 357564416 (341.0MB) OldSize = 716177408 (683.0MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 298319872 (284.5MB) used = 296272928 (282.5478820800781MB) free = 2046944 (1.952117919921875MB) 99.31384255890268% used From Space: capacity = 14680064 (14.0MB) used = 14678840 (13.998832702636719MB) free = 1224 (0.00116729736328125MB) 99.99166216169085% used To Space: capacity = 17301504 (16.5MB) used = 0 (0.0MB) free = 17301504 (16.5MB) 0.0% used PS Old Generation capacity = 716177408 (683.0MB) used = 12857208 (12.261589050292969MB) free = 703320200 (670.738410949707MB) 1.7952546193693952% used 19155 interned Strings occupying 1959520 bytes. -
jmap -histo:live 21203
打印存活的对象:
[root@hecs-402944 ~]# jmap -histo:live 21203 num #instances #bytes class name ---------------------------------------------- 1: 53132 7047872 [C 2: 11913 4617296 [B 3: 52849 1268376 java.lang.String 4: 11295 1249000 java.lang.Class 5: 29867 955744 java.util.concurrent.ConcurrentHashMap$Node 6: 6995 949352 [I 7: 13379 892552 [Ljava.lang.Object; 8: 6398 563024 java.lang.reflect.Method 9: 22623 361968 java.lang.Object 10: 8964 358560 java.util.LinkedHashMap$Entry 11: 165 350528 [Ljava.util.concurrent.ConcurrentHashMap$Node; 12: 3565 319656 [Ljava.util.HashMap$Node; 13: 9202 294464 java.util.HashMap$Node如果打印全部对象则不需要添加参数
jmap -histo 21203 -
jmap -clstats 21203
统计类加载数量
[root@hecs-402944 ~]# jmap -clstats 21203 Attaching to process ID 21203, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.332-b09 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> 3235 5770883 null live <internal> 0x00000000c07b8178 1 880 0x00000000c00036a0 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0 0x00000000c0ab89a8 1 1474 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0 0x00000000c0aba7a8 1 880 null dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0 ... ... 0x00000000c0de1a20 1 880 0x00000000c00036a0 dead sun/reflect/DelegatingClassLoader@0x000000010000a0a0 total = 131 9371 15960414 N/A alive=1, dead=130 N/A -
jmap -finalizerinfo 21203
获取等待执行finalize()的方法的对象树:
[root@hecs-402944 ~]# jmap -finalizerinfo 21203 Attaching to process ID 21203, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.332-b09 Number of objects pending for finalization: 0 -
jmap -dump:live,format=b,file=thread-21203.log 21203
将线程的堆栈的对象存活信息,按照二进制格式输出,文件名称文thread-21203.log。
[root@hecs-402944 ~]# jmap -dump:live,format=b,file=thread-21203.log 21203 Dumping heap to /root/thread-21203.log ... Heap dump file created
注意
使用jmap也是需要注意某些问题的:
-dump,-clstats等参数都会导致应用的暂停。
-histo:live获取存活存活对象,会导致jvm触发gc操作,也可能会导致暂停。
以上命令要慎重使用。