Benjamin Zhang

目前就职于中科院计算机网络信息中心,主要兴趣集中在数据库,操作系统,中间件。

【JAVA】jvm 性能调优工具之 jmap

19 Feb 2019 » JAVA

jmap概述

​ JVM Memory Map命令用于生成heap dump文件,如果不使用这个命令,还可以使用-XX:+HeapDumpOnOutOfMemoryError参数来让虚拟机出现OOM的时候自动生成dump文件。 jmap不仅能生成dump文件,还可以查询finalize执行队列、Java堆和永久代的详细信息,如当前使用率、当前使用的是哪种收集器等。【内存分析】

jmap用法

[root@app opt]# 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
    -permstat            to print permanent generation 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

这里用到了两个参数:

-dump:<dump-options>:生成堆快照。

dump-options:livedump只存活的对象。

file=zsd_java.dmpdump的文件名字为zsd_java.dmp。

jmap实例

导出dump文件,-dump

[root@app opt]# jmap -dump:format=b,file=zsd_java.dmp 12198
Dumping heap to /opt/zsd_java.dmp ...

可以通过jmap命令,对上述dmp文件进行分析,命令如下:

[root@web opt]# jhat -port 5000 zsd_java.dmp
Reading from zsd_java.dmp...
Dump file created Tue Feb 19 17:12:56 CST 2019
Snapshot read, resolving...
Resolving 10101780 objects...
Chasing references, expect 2020 dots...............................................................................................
Eliminating duplicate references..........................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 5000
Server is ready.

可以打开浏览器http://172.30.xxx.30:5000/来查看具体的细节。

jmap -histo:live

查看堆内存中的对象的情况。如果有大量对象在持续被引用,并没有被释放掉,那就产生了内存泄露,就要结合代码,把不用的对象释放掉。

[root@app opt]# jmap -histo:live 12198 |more

 num     #instances         #bytes  class name
----------------------------------------------
   1:       1911100      250641680  [C
   2:         14299      168493144  [B
   3:       3214819      102874208  java.util.concurrent.ConcurrentHashMap$Node
   4:       3105643       99380576  com.finebi.cube.location.meta.BILocationInfoImp
   5:       1705990       81887520  java.util.HashMap
   6:       1641853       39404472  java.lang.String
   7:        562474       34379776  [I
   8:        856563       34262520  java.util.LinkedHashMap$Entry
   9:       1677919       26846704  java.util.HashSet
  10:         11087       23213904  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  11:        748737       17969688  com.fr.data.Dictionary$MV
  12:        245852       15734528  java.util.concurrent.ConcurrentHashMap
  13:        232700       14892800  com.fr.fs.cache.RoleCacheInfo
  14:        234590       13137040  com.fr.fs.base.entity.CompanyRole
  15:        272664       13087872  com.fr.third.pdf.io.font.otf.Glyph
  16:         45150       12269408  [Ljava.util.HashMap$Node;
  17:        500076       12001824  com.fr.stable.ColumnRow
  18:        567421        9078736  java.lang.Integer
  19:         94015        8706232  [Ljava.lang.Object;
  20:        261267        8360544  java.util.Hashtable$Entry
  21:         85589        6847120  java.net.URI
  22:           374        6445424  [D
  23:        262219        6293256  java.lang.Long
  24:        186813        5978016  java.util.HashMap$Node
  25:         89896        4315008  java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync
  26:          1261        2457704  [Ljava.util.Hashtable$Entry;
  27:         19845        2242152  java.lang.Class
  28:         87826        2107824  java.util.concurrent.locks.ReentrantReadWriteLock
  29:            51        2000816  [Lcom.fr.stable.ColumnRow;
  30:         82543        1981032  com.finebi.common.name.NameImp

jmap -heap

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

[root@web ~]# jmap -finalizerinfo 16916
Attaching to process ID 16916, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13
Number of objects pending for finalization: 0
[root@web ~]# jmap -heap 16916
Attaching to process ID 16916, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13

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

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 4215275520 (4020.0MB)
   NewSize                  = 88080384 (84.0MB)
   MaxNewSize               = 1405091840 (1340.0MB)
   OldSize                  = 176160768 (168.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 = 634912768 (605.5MB)
   used     = 196229312 (187.13885498046875MB)
   free     = 438683456 (418.36114501953125MB)
   30.906499583892444% used
From Space:
   capacity = 208666624 (199.0MB)
   used     = 130377184 (124.33737182617188MB)
   free     = 78289440 (74.66262817382812MB)
   62.48109136993561% used
To Space:
   capacity = 240648192 (229.5MB)
   used     = 0 (0.0MB)
   free     = 240648192 (229.5MB)
   0.0% used
PS Old Generation
   capacity = 848297984 (809.0MB)
   used     = 611953336 (583.6041793823242MB)
   free     = 236344648 (225.39582061767578MB)
   72.13895913254935% used

128235 interned Strings occupying 12400744 bytes.

jmap -finalizerinfo pid 打印等待终结的对象信息

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