功能介绍

jmap(Memory Map for Java)命令用于生成堆转储快照(一般称为heapdump或dump文件)。

jmap的作用并不仅仅是为了获取dump文件,它还可以查询finalize执行队列、Java堆和永久代的详细信息,如 空间使用率、当前用的是哪种收集器等。


jmap命令格式

jmap[option]vmid

jmap工具主要选项

选项作用
<none> 无参数
-heap显示java堆详细信息,如使用哪种回收器、参数配置、分带情况等。
-histo[:live]显示堆中对象统计信息,包括类、实例数量、合计容量
-clstats-clstats是-permstat的替代方案,显示类装载器状态
-finalizerinfo显示在F-Queue中等待Finalizer线程执行finalize方法的对象
-dump:<dump-options>生成java堆快照,<ump-options>在help命令下查看格式
-F强制生成快照
-h | -help帮助命令
-J<flag>指定传递给运行jmap的JVM的参数


Example

-help
C:\Users\li>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



-dump

dump堆到文件,format指定输出格式,live指明是活着的对象,file指定文件名,导出的文件可以通过工具进行分析

C:\Users\li>jmap -dump:live,format=b,file=dump.hprof  9948
Dumping heap to C:\Users\li\dump.hprof ...
Heap dump file created



-heap

查看java 堆(heap)使用情况

C:\Users\li>jmap -heap 9948
Attaching to process ID 9948, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.45-b02

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

Heap Configuration:                                 //堆内存初始化配置
   MinHeapFreeRatio         = 0                     //-XX:MinHeapFreeRatio设置JVM堆最小空闲比率
   MaxHeapFreeRatio         = 100                   //-XX:MaxHeapFreeRatio设置JVM堆最大空闲比率
   MaxHeapSize              = 2132803584 (2034.0MB) //-XX:MaxHeapSize=设置JVM堆的最大大小
   NewSize                  = 44564480 (42.5MB)     //-XX:NewSize=设置JVM堆的'新生代'的默认(起始)大小
   MaxNewSize               = 710934528 (678.0MB)   //-XX:MaxNewSize=设置JVM堆的'新生代'的最大大小
   OldSize                  = 89653248 (85.5MB)     //-XX:OldSize=设置JVM堆的'老年代'的大小
   NewRatio                 = 2                     //-XX:NewRatio=:'新生代'和'老年代'的大小比率1:2
   SurvivorRatio            = 8                     //-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值8:1:1
   MetaspaceSize            = 21807104 (20.796875MB)  //-XX:MaxPermSize=256m 设置元空间大小
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB     //-XX:MaxMetaspaceSize=256m;
   G1HeapRegionSize         = 0 (0.0MB)  //G1的参数,不太明白 参考:http://bboniao.com/jvm/2014-03/g1garbage-first.html

Heap Usage:                                    //堆内存使用情况
PS Young Generation                            //新生代
Eden Space:                                    //Eden区分布 
   capacity = 176160768 (168.0MB)              //总空间
   used     = 6222416 (5.9341583251953125MB)   //已用空间
   free     = 169938352 (162.0658416748047MB)  //空闲空间
   3.532237098330543% used                     //使用比率        
From Space:                                    //survivor From区分布
   capacity = 16777216 (16.0MB)                
   used     = 0 (0.0MB)                        
   free     = 16777216 (16.0MB)                
   0.0% used                                   
To Space:                                      //survivor To区分布
   capacity = 17301504 (16.5MB)
   used     = 0 (0.0MB)
   free     = 17301504 (16.5MB)
   0.0% used
PS Old Generation                              //老年代分布
   capacity = 164626432 (157.0MB)
   used     = 17516744 (16.70526885986328MB)
   free     = 147109688 (140.29473114013672MB)
   10.640298636855594% used

15941 interned Strings occupying 2135928 bytes.

-histo

显示堆中对象统计信息,包括类、实例数量、合计容量,信息量太大,这里只列举出来几个

C:\Users\li>jmap -histo 9948

 num     #instances         #bytes  class name
----------------------------------------------
   1:        147489       23049616  [C
   2:         43385       10243776  [B
   3:          8896        8142248  [I
   4:         29747        2617736  java.lang.reflect.Method
   5:         88435        2122440  java.lang.String
   6:         81669        1725912  [Ljava.lang.Class;
   7:         19397        1533912  [Ljava.util.HashMap$Node;
   8:         32332        1034624  java.util.HashMap$Node
   9:         17942         960656  [Ljava.lang.Object;
  10:          7434         825536  java.lang.Class
  11:         24911         797152  java.util.concurrent.ConcurrentHashMap$Node
  12:         24200         774400  java.util.ArrayList$Itr
  13:         14904         715392  java.util.HashMap
  14:         12567         703752  java.util.LinkedHashMap
  15:         16469         658760  java.util.LinkedHashMap$Entry
  16:

jmap -histo:live 这个命令执行,统计或者的对象 ,JVM会先触发gc,然后再统计信息。



-clstats

打印类加载器的统计信息(取代了在JDK8之前打印类加载器信息的permstat)。对于每个类加载器而言,它的名称、活跃度、地址、父类加载器、它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。

C:\Users\li>jmap -clstats 9948
Attaching to process ID 9948, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.45-b02
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>     2426    4182766   null          live    <internal>
0x00000000814781c0      1       880     0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081478c40      1       1473    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000814679c8      1       880     0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081479348      1       1473    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x000000008147d548      1       1472    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081478750      1       1476    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081478dd0      1       1473    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081229238      8       15658     null          live    sun/misc/Launcher$ExtClassLoader@0x000000010000fa10
0x0000000081467b58      1       880     0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000814794d8      1       1473    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081d14970      1       880     0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081d15b70      1       1505      null          dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081ea28c8      1       880       null          dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000080e68c40      0       0       0x00000000812291d8      live    java/util/ResourceBundle$RBClassLoader@0x00000001000bc0d8
0x0000000081478f60      1       1476    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081467ce8      1       880     0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081cfabe0      1       1474    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x0000000081ea2a58      1       1474    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000814673f0      1       880     0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x00000000814790f0      1       1473    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0x000000008147af70      1       1471    0x00000000812291d8      dead    sun/reflect/DelegatingClassLoader@0x0000000100009df8
0

-finalizerinfo

显示在F-Queue中等待Finalizer线程执行finalize方法的对象,对象自救的时候掉的finalize方法

C:\Users\li>jmap -finalizerinfo 9948
Attaching to process ID 9948, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.45-b02
Number of objects pending for finalization: 0
Number of objects pending for finalization: 0 说明当前F-QUEUE队列中并没有等待Fializer线程执行finalizer方法的对象。


-F

强制模式。如果指定的pid没有响应,请使用jmap -dump或jmap -histo选项。此模式下,不支持live子选项。



参考:

https://yq.aliyun.com/articles/44642

http://www.importnew.com/18196.html

https://www.cnblogs.com/myna/p/7573843.html

关于G1的

http://bboniao.com/jvm/2014-03/g1garbage-first.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐