jmap它可以生成 java 程序的 dump 文件, 也可以查看堆内对象示例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。
C:\Users\cmcc>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
histogram为:(统计学的) 直方图,矩形图;
参数:
- 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:<dump-options>:生成堆转储快照
- F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
- help:打印帮助信息
- J<flag>:指定传递给运行jmap的JVM的参数
实例1:
6440进程的java程序的对象统计信息,并输出到文件中;
C:\Users\cmcc>
C:\Users\cmcc>jmap -histo 6440 >C:\Users\cmcc\Desktop\histogram.txt
num #instances #bytes class name
----------------------------------------------
1: 18497 16858104 [I
2: 115870 15030416 [C
3: 31071 8540160 [B
4: 54418 2176720 java.util.TreeMap$Entry
5: 84204 2020896 java.lang.String
2326: 1 16 sun.util.resources.LocaleData$LocaleDataResourceBundleControl
2327: 1 16 websocket.drawboard.DrawboardContextListener
Total 638907 57214128
实例2:jmap -heap 6440 展示堆的统计信息
C:\Users\cmcc>jmap -heap 6440
Attaching to process ID 6440, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.91-b15
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 536870912 (512.0MB)
NewSize = 268435456 (256.0MB)
MaxNewSize = 268435456 (256.0MB)
OldSize = 268435456 (256.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 = 182452224 (174.0MB)
used = 40148728 (38.28881072998047MB)
free = 142303496 (135.71118927001953MB)
22.00506363791981% used
From Space:
capacity = 39845888 (38.0MB)
used = 0 (0.0MB)
free = 39845888 (38.0MB)
0.0% used
To Space:
capacity = 42991616 (41.0MB)
used = 0 (0.0MB)
free = 42991616 (41.0MB)
0.0% used
PS Old Generation
capacity = 268435456 (256.0MB)
used = 18282064 (17.435134887695312MB)
free = 250153392 (238.5648651123047MB)
6.8105995655059814% used
16417 interned Strings occupying 2095832 bytes.
实例3、获取java程序当前快照
以hprof二进制格式转储Java堆到指定filename的文件中
C:\Users\cmcc>jmap -dump:format=b,file=C:\Users\cmcc\Desktop\heap.hprof 6440
Dumping heap to C:\Users\cmcc\Desktop\heap.hprof ...
Heap dump file created
实例4、jmap -clstats 6440
描述:打印类加载器信息
-clstats是-permstat的替代方案,在JDK8之前,-permstat用来打印类加载器的数据
打印Java堆内存的永久保存区域的类加载器的智能统计信息。对于每个类加载器而言,它的名称、活跃度、地址、父类加载器、它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。
C:\Users\cmcc>jmap -clstats 6440
Attaching to process ID 6440, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.91-b15
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness.....................liveness analysis may be in
accurate ...
class_loader classes bytes parent_loader alive? type
<bootstrap> 2501 4311871 null live <internal>
0x00000000e05a2358 1 1473 0x00000000e0150ad8 dead sun/refl
ect/DelegatingClassLoader@0x0000000013209df8
0x00000000e05a3858 1 1473 0x00000000e0150ad8 dead sun/refl
total = 84 6430 12703168 N/A alive=14, dead=70
N/A
实例5、jmap -finalizerinfo 6440
描述:打印等待终结的对象信息
通过jmap可以观察finalizer队列中的对象,一个不恰当的finalize()方法可能导致对象堆积在finalizer队列中,使用一下命令可以查看堆积在finalizer对象中的对象。
C:\Users\cmcc>jmap -finalizerinfo 6440
Attaching to process ID 6440, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.91-b15
Number of objects pending for finalization: 0
C:\Users\cmcc>
Number of objects pending for finalization: 0 说明当前F-QUEUE队列中并没有等待Fializer线程执行final