Java内存分析工具jmap

1. jmap

1.1 概述

JVM Memory Map命令用于生成heap dump文件,如果不使用这个命令,还可以使用-XX:+HeapDumpOnOutOfMemoryError参数来让虚拟机出现OOM的时候自动生成dump文件。

jmap不仅能生成dump文件,还可以查询finalize执行队列、Java堆和永久代的详细信息,如当前使用率、当前使用的是哪种收集器等。

> jmap
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

1.2 参数

option:选项参数,不可同时使用多个选项参数。

pid:Java进程id。

executable:产生核心dump的Java可执行文件。

core:需要打印配置信息的核心文件。

remote-hostname-or-ip:远程调试的主机名或ip。

server-id:可选的唯一id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标示服务器。

1.3 options参数

heap:显示Java堆详细信息;

histo:线下堆中对象的统计信息;

permstat:Java堆中内存的永久保存区域的类加载器的统计信息;

finalizerinfo:显示在F-Queue队列等待Finlizer线程执行finalizer方法的对象;

dump:生成堆转储快照;

F:当-dump没有响应时,强制生成dump快照;

2. 用法

2.1jmap -dump:live,format=b,file=dump.hprof pid

dump堆到文件,format指定输出格式,live指明是活着的对象,file指定文件名。

> jmap -dump:live,format=b,file=dump.hprof 129665
Dumping heap to /opt/huawei/inputMethod/flight/flight/dump.hprof ...
Heap dump file created

dump.hprof这个文件可以通过eclipse的打开:

Java内存分析工具jmap

2.2 jmap -heap pid

打印heap的概要信息,GC使用的算法,heap的配置和使用情况,可以用此来判断内存目前的使用情况以及垃圾回收情况。

> jmap -heap 129665
Attaching to process ID 129665, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13

using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 2147483648 (2048.0MB)
   NewSize                  = 805306368 (768.0MB)
   MaxNewSize               = 805306368 (768.0MB)
   OldSize                  = 1342177280 (1280.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 724828160 (691.25MB)
   used     = 291033744 (277.55140686035156MB)
   free     = 433794416 (413.69859313964844MB)
   40.15210225827871% used
Eden Space:
   capacity = 644349952 (614.5MB)
   used     = 291033744 (277.55140686035156MB)
   free     = 353316208 (336.94859313964844MB)
   45.167031222189024% used
From Space:
   capacity = 80478208 (76.75MB)
   used     = 0 (0.0MB)
   free     = 80478208 (76.75MB)
   0.0% used
To Space:
   capacity = 80478208 (76.75MB)
   used     = 0 (0.0MB)
   free     = 80478208 (76.75MB)
   0.0% used
concurrent mark-sweep generation:
   capacity = 1342177280 (1280.0MB)
   used     = 4730600 (4.511451721191406MB)
   free     = 1337446680 (1275.4885482788086MB)
   0.3524571657180786% used

7119 interned Strings occupying 644232 bytes.

2.3 jmap -finalizerinfo pid

打印等待回收的对象信息。

> jmap -finalizerinfo 129665
Attaching to process ID 129665, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13
Number of objects pending for finalization: 0

Number of objects pending for finalization:0 说明当前F-Queue队列中并没有等待Finalizer线程执行finalizer方法的对象。

相关推荐