JVM 性能调优工具 jmap

jmap 用法

$ 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

参数

  • 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::生成堆转储快照
  • F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
  • help:打印帮助信息
  • J:指定传递给运行jmap的JVM的参数

示例一:no option

查看进程的内存映像信息,类似 Solaris pmap 命令。

jmap <pid>

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

Attaching to process ID 4628, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12
0x000000006a280000      8828K   D:\Program Files\Java\jdk1.8.0_161\jre\bin\server\jvm.dll
0x000000006c1c0000      840K    D:\Program Files\Java\jdk1.8.0_161\jre\bin\msvcr100.dll
0x000000006c350000      68K     D:\Program Files\Java\jdk1.8.0_161\jre\bin\nio.dll
0x000000006c370000      104K    D:\Program Files\Java\jdk1.8.0_161\jre\bin\net.dll
0x000000006c390000      88K     D:\Program Files\Java\jdk1.8.0_161\jre\bin\zip.dll
0x000000006c3b0000      164K    D:\Program Files\Java\jdk1.8.0_161\jre\bin\java.dll
0x000000006c3e0000      60K     D:\Program Files\Java\jdk1.8.0_161\jre\bin\verify.dll
0x00007ff70beb0000      220K    D:\Program Files\Java\jdk1.8.0_161\bin\java.exe
0x00007ff98fea0000      36K     C:\WINDOWS\SYSTEM32\WSOCK32.dll

示例二:heap

显示Java堆详细信息。

jmap -heap <pid>

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

Attaching to process ID 4628, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12

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

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 734003200 (700.0MB)
   NewSize                  = 111673344 (106.5MB)
   MaxNewSize               = 244318208 (233.0MB)
   OldSize                  = 223870976 (213.5MB)
   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 = 84410368 (80.5MB)
   used     = 0 (0.0MB)
   free     = 84410368 (80.5MB)
   0.0% used
From Space:
   capacity = 13631488 (13.0MB)
   used     = 0 (0.0MB)
   free     = 13631488 (13.0MB)
   0.0% used
To Space:
   capacity = 13631488 (13.0MB)
   used     = 0 (0.0MB)
   free     = 13631488 (13.0MB)
   0.0% used
PS Old Generation
   capacity = 123731968 (118.0MB)
   used     = 20986936 (20.01470184326172MB)
   free     = 102745032 (97.98529815673828MB)
   16.96161173157773% used

5082 interned Strings occupying 448296 bytes.

示例三:histo[:live]

显示堆中对象的统计信息。

jmap -histo:live <pid>

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

 num     #instances         #bytes  class name
----------------------------------------------
   1:           649       17000784  [B
   2:         15489        1462736  [C
   3:          3603         402048  java.lang.Class
   4:         15306         367344  java.lang.String
   5:          4232         363480  [Ljava.lang.Object;
   6:          6938         222016  java.util.concurrent.ConcurrentHashMap$Node
   7:          6447         103152  java.lang.Object
   8:          3026          96832  java.util.HashMap$Node
   9:          1418          68808  [I
  10:            47          58064  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  11:           788          37824  gnu.trove.THashMap
  12:           210          33792  [Ljava.util.HashMap$Node;
  13:           841          33640  java.util.TreeMap$Entry
  14:           434          27776  io.netty.buffer.PoolSubpage
  15:           530          21200  java.lang.ref.SoftReference

示例四:clstats

打印类加载器信息。

jmap -clstats <pid>

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

Attaching to process ID 4628, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12
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>     1436    2534882   null          live    <internal>
0x00000000d5414f20      2099    3484055 0x00000000d54398c8      live    java/net/URLClassLoader@0x000000010000ecd0
0x00000000d5751b60      0       0       0x00000000d54398c8      dead    java/util/ResourceBundle$RBClassLoader@0x0000000100093bc8
0x00000000d54398c8      25      101450  0x00000000d5439928      live    sun/misc/Launcher$AppClassLoader@0x000000010000f6a0
0x00000000d5439928      3       2574      null          live    sun/misc/Launcher$ExtClassLoader@0x000000010000fa48

total = 5       3563    6122961     N/A         alive=4, dead=1     N/A

示例五:finalizerinfo

打印等待终结的对象信息。

jmap -finalizerinfo <pid>
Attaching to process ID 4628, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12
Number of objects pending for finalization: 0

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

示例六:dump[:dump-options]

生成堆转储快照dump文件。

jmap -dump:format=b,file=heapdump.phrof <pid>

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

$ jmap -dump:format=b,file=heapdump.phrof 4628
Dumping heap to C:\Users\Victor.Zxy\heapdump.phrof ...
Heap dump file created

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

上一篇:jvm指令介绍及在线debug工具原理


下一篇:EditText setHint()输入提示你知道吗?