1. 概述

1.1. 工具简介

jmap(Memory Map for Java,Java内存映像工具),用于分析Java虚拟机的内存对象信息;jmap可用于生成虚拟机内存快照(dump文件),还可以查询finalize执行队列、Java堆和永久带的详细信息;

1.2. 命令帮助

wanghui@Mokalas-MacBook-Pro ~ $ 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

2. 命令常用选项

2.1. -dump

用于生成Java堆存储快照 命令格式为:-dump:[live,],format=b,file=<filename>,其中live子参数说明是否只dump出存活对象;

PS:当使用-dump选项没响应时,可使用-F参数强制dump快照;

2.2. -finalizerinfo

显示F-Queue中等待Finalizer线程执行finalize方法的对象,只在Linux平台下有效

2.3. -heap

显示Java堆的详细信息,如使用哪种回收器、参数配置、分代状况等,只在Linux下有效

wanghui@Mokalas-MacBook-Pro ~ $ jmap -heap 1695
Attaching to process ID 1695, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.101-b13

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

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 4294967296 (4096.0MB)
   NewSize                  = 89128960 (85.0MB)
   MaxNewSize               = 1431306240 (1365.0MB)
   OldSize                  = 179306496 (171.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 = 67108864 (64.0MB)
   used     = 25621264 (24.434341430664062MB)
   free     = 41487600 (39.56565856933594MB)
   38.1786584854126% used
From Space:
   capacity = 11010048 (10.5MB)
   used     = 0 (0.0MB)
   free     = 11010048 (10.5MB)
   0.0% used
To Space:
   capacity = 11010048 (10.5MB)
   used     = 0 (0.0MB)
   free     = 11010048 (10.5MB)
   0.0% used
PS Old Generation
   capacity = 179306496 (171.0MB)
   used     = 0 (0.0MB)
   free     = 179306496 (171.0MB)
   0.0% used

2278 interned Strings occupying 159152 bytes.
wanghui@Mokalas-MacBook-Pro ~ $

2.4. -histo

用于显示堆中对象的统计信息,如类、实例的数量、容量等

wanghui@Mokalas-MacBook-Pro ~ $ jmap -histo 1695

 num     #instances         #bytes  class name
----------------------------------------------
   1:          1920       16299408  [I
   2:         10740        6832000  [B
   3:         23690        2307984  [C
   4:         14765         354360  java.lang.String
   5:          1908         136488  [Ljava.lang.Object;
   6:          1144         133056  java.lang.Class
   7:          2739         131472  java.util.HashMap
   8:           782          62560  [S
   9:          1337          42784  java.util.concurrent.ConcurrentHashMap$Node
  10:          2625          42000  java.util.HashSet
  11:          1461          35064  java.lang.StringBuilder
  12:           336          32256  java.util.jar.JarFile$JarFileEntry
  13:           738          29520  java.lang.ref.Finalizer
  14:           346          27680  java.util.zip.ZipEntry
  15:           837          26784  java.util.HashMap$Node
  16:           418          26752  java.net.URL
  ......
Total         77063       26963816

2.5. -clstat

统计ClassLoader的使用情况

wanghui@Mokalas-MacBook-Pro ~ $ jmap -clstats 1695
Attaching to process ID 1695, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.101-b13
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness...........done.
class_loader  classes bytes parent_loader alive?  type

<bootstrap> 715 1324745   null    live  <internal>
0x000000076ab3dc50  0 0   null    live  sun/misc/Launcher$ExtClassLoader@0x00000007c000fa30
0x000000076ab5d058  479 1160211 0x000000076ab3dc50  live  sun/misc/Launcher$AppClassLoader@0x00000007c000f688

total = 3 1194  2484956     N/A     alive=3, dead=0     N/A

2.6. -permstat

以ClassLoader为统计口径显示永久带内存状态,只在LInux下有效(未看到提供)

3. 最佳实践

3.1. 获取Java队存储快照

在不使用jmap的情况下可以通过暴力方法获取堆存储快照,应用启动时增加参数+XX:+HeapDumpOnOutOfMemoryError参数,当虚拟机OOM异常时会自动dump出堆存储文件;

再则,通过指定XX:+HeapDumpOnCtrlBreank参数则可使用Ctrl+Break键让VM生生成dump文件,或者在Linux系统下通过kill -3命令吓唬VM也能拿到dump文件;

Copyright © 辉辉大侠@杭州 2017 all right reserved,powered by Gitbook修订时间: 2017-03-30 01:17:50

results matching ""

    No results matching ""