jvm 性能调优工具之 jmap

本文详细介绍了jmap命令的各种功能,包括生成Java程序的dump文件、查看堆内对象统计信息、展示类加载器详情及F-Queue队列信息。通过多个实例展示了如何使用jmap进行Java应用程序的内存诊断。

概述

命令jmap是一个多功能的命令。它可以生成 java 程序的 dump 文件, 也可以查看堆内对象示例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。

jmap 用法

[root@localhost ~]# 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
[root@localhost ~]# 
参数:
  • option: 选项参数。
  • pid: 需要打印配置信息的进程ID。
  • executable: 产生核心dump的Java可执行文件。
  • core: 需要打印配置信息的核心文件。
  • server-id 可选的唯一id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标识服务器。
  • remote server IP or hostname 远程调试服务器的IP地址或主机名。
option
  • no option: 查看进程的内存映像信息,类似 Solaris pmap 命令。
  • heap: 显示Java堆详细信息
  • histo[:live]: 显示堆中对象的统计信息
  • clstats:打印类加载器信息
  • finalizerinfo: 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
  • dump:<dump-options>:生成堆转储快照
  • F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
  • help:打印帮助信息
  • J<flag>:指定传递给运行jmap的JVM的参数

示例一:no option

命令:jmap pid
描述:查看进程的内存映像信息,类似 Solaris pmap 命令。

使用不带选项参数的jmap打印共享对象映射,将会打印目标虚拟机中加载的每个共享对象的起始地址、映射大小以及共享对象文件的路径全称。这与Solaris的pmap工具比较相似。

[root@localhost ~]# jmap 9824
Attaching to process ID 9824, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.201-b09
0x0000000000400000	8K	/home/jdk/bin/java
0x0000003c2de00000	157K	/lib64/ld-2.12.so
0x0000003c2e200000	1885K	/lib64/libc-2.12.so
0x0000003c2e600000	143K	/lib64/libpthread-2.12.so
0x0000003c2ea00000	22K	/lib64/libdl-2.12.so
0x0000003c2ee00000	46K	/lib64/librt-2.12.so
0x0000003c2f200000	585K	/lib64/libm-2.12.so
0x0000003c39200000	91K	/lib64/libgcc_s-4.4.7-20120601.so.1
0x00007fa996d92000	50K	/home/jdk/jre/lib/amd64/libmanagement.so
0x00007fa99739c000	276K	/home/jdk/jre/lib/amd64/libsunec.so
0x00007fa9975da000	114K	/home/jdk/jre/lib/amd64/libnet.so
0x00007fa9977f2000	91K	/home/jdk/jre/lib/amd64/libnio.so
0x00007fa9cb62c000	124K	/home/jdk/jre/lib/amd64/libzip.so
0x00007fa9cb848000	64K	/lib64/libnss_files-2.12.so
0x00007fa9cba67000	226K	/home/jdk/jre/lib/amd64/libjava.so
0x00007fa9cbc96000	64K	/home/jdk/jre/lib/amd64/libverify.so
0x00007fa9cbfa6000	16645K	/home/jdk/jre/lib/amd64/server/libjvm.so
0x00007fa9ccf92000	106K	/home/jdk/lib/amd64/jli/libjli.so
[root@localhost ~]# 

示例二:heap

命令:jmap -heap pid
描述:显示Java堆详细信息

打印一个堆的摘要信息,包括使用的GC算法、堆配置信息和各内存区域内存使用信息

[root@localhost ~]# jmap -heap 9824
Attaching to process ID 9824, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.201-b09

using thread-local object allocation.
Parallel GC with 8 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 2048917504 (1954.0MB)
   NewSize                  = 42991616 (41.0MB)
   MaxNewSize               = 682622976 (651.0MB)
   OldSize                  = 87031808 (83.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 = 57671680 (55.0MB)
   used     = 15361488 (14.649856567382812MB)
   free     = 42310192 (40.35014343261719MB)
   26.636102849786933% used
From Space:
   capacity = 2097152 (2.0MB)
   used     = 1417568 (1.351898193359375MB)
   free     = 679584 (0.648101806640625MB)
   67.59490966796875% used
To Space:
   capacity = 2097152 (2.0MB)
   used     = 0 (0.0MB)
   free     = 2097152 (2.0MB)
   0.0% used
PS Old Generation
   capacity = 196083712 (187.0MB)
   used     = 57017984 (54.3765869140625MB)
   free     = 139065728 (132.6234130859375MB)
   29.078388724097593% used

19040 interned Strings occupying 2101824 bytes.
[root@localhost ~]# 

示例三:histo[:live]

命令:jmap -histo:live pid
描述:显示堆中对象的统计信息

其中包括每个Java类、对象数量、内存大小(单位:字节)、完全限定的类名。打印的虚拟机内部的类名称将会带有一个’*’前缀。如果指定了live子选项,则只计算活动的对象。


[root@localhost ~]# jmap -histo:live 10027

 num     #instances         #bytes  class name
----------------------------------------------
   1:         12160         857048  [C
   2:          1164         395200  [B
   3:          2687         307408  java.lang.Class
   4:         12145         291480  java.lang.String
   5:          2315         146336  [Ljava.lang.Object;
   6:          1303         114664  java.lang.reflect.Method
   7:          1588          80424  [I
   8:          1980          63360  java.util.HashMap$Node
   9:          1689          54048  java.util.concurrent.ConcurrentHashMap$Node
  10:          1309          52360  java.util.LinkedHashMap$Entry
  11:           264          38080  [Ljava.util.HashMap$Node;
  12:          1098          35136  java.util.Hashtable$Entry
  13:          1971          31536  java.lang.Object
  14:          1233          29096  [Ljava.lang.Class;
  15:           571          22840  java.lang.ref.SoftReference
  16:           527          17776  [Ljava.lang.String;
  17:           424          16960  java.math.BigInteger
  18:           625          15000  java.util.concurrent.atomic.AtomicLong
  19:           301          14448  java.util.HashMap
  20:            50          13712  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  21:           413          13216  java.lang.ref.WeakReference

示例四:clstats

命令:jmap -clstats pid
描述:打印类加载器信息

-clstats是-permstat的替代方案,在JDK8之前,-permstat用来打印类加载器的数据
打印Java堆内存的永久保存区域的类加载器的智能统计信息。对于每个类加载器而言,它的名称、活跃度、地址、父类加载器、它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。

[root@localhost ~]# jmap -clstats 10027
Attaching to process ID 10027, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.201-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>	1946	3496011	  null  	live	<internal>
0x00000000d7535050	1	1471	0x0000000085e13ac8	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000860c1688	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d7542d90	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x000000008605a340	1	880	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d75fc4d8	1	1473	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x0000000085e13b28	28	46144	  null  	dead	sun/misc/Launcher$ExtClassLoader@0x000000010000fc80
0x00000000860c15c0	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x000000008605a408	1	880	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000860c1818	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d7569ec0	1	1471	0x0000000085e13ac8	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d7571e40	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d75047c8	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d7589640	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d7510388	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d753e3c8	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000860c1750	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d75603c8	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d757c248	1	1471	0x0000000085e13ac8	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x000000008605a020	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d7557530	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d75d0078	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x0000000085f82050	0	0	0x0000000085e13ac8	dead	java/util/ResourceBundle$RBClassLoader@0x00000001000735e0
0x00000000d750aab8	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x0000000085e13ac8	391	676812	0x0000000085e13b28	dead	sun/misc/Launcher$AppClassLoader@0x000000010000f8d8
0x00000000d752bd38	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000860c18e0	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d75498f8	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x000000008605a0e8	1	1474	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x000000008605a1b0	1	880	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x00000000d758ec60	1	1471	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028
0x000000008605a278	1	880	  null  	dead	sun/reflect/DelegatingClassLoader@0x000000010000a028

total = 32	2393	4257796	    N/A    	alive=1, dead=31	    N/A    
[root@localhost ~]# 

示例五:finalizerinfo

命令:jmap -finalizerinfo pid
描述:打印等待终结的对象信息

[root@localhost ~]# jmap -finalizerinfo 10027
Attaching to process ID 10027, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.201-b09
Number of objects pending for finalization: 0
[root@localhost ~]# 

Number of objects pending for finalization: 0 说明当前F-QUEUE队列中并没有等待Fializer线程执行final

示例六:dump:<dump-options>

命令:jmap -dump:format=b,file=heapdump.phrof pid
描述:生成堆转储快照dump文件。

以hprof二进制格式转储Java堆到指定filename的文件中。live子选项是可选的。如果指定了live子选项,堆中只有活动的对象会被转储。想要浏览heap dump,你可以使用jhat(Java堆分析工具)读取生成的文件。

这个命令执行,JVM会将整个heap的信息dump写入到一个文件,heap如果比较大的话,就会导致这个过程比较耗时,并且执行的过程中为了保证dump的信息是可靠的,所以会暂停应用, 线上系统慎用。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值