内存监控工具——jmap
一、jmap的作用
监控内存内的Java对象
二、语法
jmap [option] < pid >
说明:
option:命令选项,
三、创建一个java程序
Test类为死循环,让这个程序一直运行。代码如下:
package com;
public class Test extends Thread{
public static void main(String[] args) {
Test t = new Test();
long i = 1;
while(true) {
System.out.println("现在是:--->"+i);
i++;
try {
t.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
四、jps
在cmd中使用jps命令用于输出JVM进程状态信息
C:\Users\99273>jps
11644
13212 Jps
14572 Test
五、jmap命令选项
-help/-h
jmap -h或jmap -help用于查看jmap所有可用命令及作用,如下所示:
C:\Users\99273>jmap -h
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
C:\Users\99273>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
-heap
打印Java堆概要信息,包括使用的GC算法、堆配置参数和各代中堆内存使用情况;
C:\Users\99273>jmap -heap 1740
Attaching to process ID 1740, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 25.131-b11
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 268435456 (256.0MB)
NewSize = 5570560 (5.3125MB)
MaxNewSize = 89456640 (85.3125MB)
OldSize = 11206656 (10.6875MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 12582912 (12.0MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 4294901760 (4095.9375MB)
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 5046272 (4.8125MB)
used = 996840 (0.9506607055664062MB)
free = 4049432 (3.8618392944335938MB)
19.753988687094157% used
Eden Space:
capacity = 4521984 (4.3125MB)
used = 996840 (0.9506607055664062MB)
free = 3525144 (3.3618392944335938MB)
22.04430621603261% used
From Space:
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
To Space:
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
tenured generation:
capacity = 11206656 (10.6875MB)
used = 0 (0.0MB)
free = 11206656 (10.6875MB)
0.0% used
1623 interned Strings occupying 129264 bytes.
-histo[:live]
打印Java堆中对象直方图,通过该图可以获取每个class的对象数目,占用内存大小和类全名信息,带上:live,则只统计活着的对象,
C:\Users\99273>jmap -histo 1740
num #instances #bytes class name
----------------------------------------------
1: 2371 291056 [C
2: 153 83304 [I
3: 510 49944 java.lang.Class
4: 2126 34016 java.lang.String
5: 538 29080 [Ljava.lang.Object;
6: 835 26720 java.util.TreeMap$Entry
7: 15 25072 [B
8: 206 9888 java.nio.HeapCharBuffer
9: 196 7296 [Ljava.lang.String;
…………
193: 1 8 sun.misc.Unsafe
194: 1 8 sun.net.www.protocol.file.Handler
195: 1 8 sun.reflect.ReflectionFactory
Total 8473 606344
带上alive
C:\Users\99273>jmap -histo:live 1740
num #instances #bytes class name
----------------------------------------------
1: 2165 280344 [C
2: 510 49944 java.lang.Class
3: 2023 32368 java.lang.String
4: 538 29080 [Ljava.lang.Object;
5: 835 26720 java.util.TreeMap$Entry
6: 15 25072 [B
7: 152 15368 [I
8: 196 7296 [Ljava.lang.String;
9: 256 4096 java.lang.Integer
……
192: 1 8 sun.misc.Unsafe
193: 1 8 sun.net.www.protocol.file.Handler
194: 1 8 sun.reflect.ReflectionFactory
Total 7854 514512
class name列出现了[C、[B、[L等很奇怪的内容,这些属于非自定义类,具体为:
BaseType Character | Type | Interpretation |
---|---|---|
B | byte | signed byte |
C | char | Unicode character |
D | double | double-precision floating-point value |
F | float | single-precision floating-point value |
I | int | integer |
J | long | long integer |
L | reference | an instance of class |
S | short | signed short |
Z | boolean | true or false |
[ | reference | one array dimension |
-permstat
打印永久代统计信息,注意:从JDK8开始,Metaspace(元空间)替代了永久代, 高版本jdk将得不到统计信息,
-finalizerinfo
打印等待回收的对象信息,如下命令:
C:\Users\99273>jmap -finalizerinfo 1740
Attaching to process ID 1740, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 25.131-b11
Number of objects pending for finalization: 0
Number of objects pending for finalization: 0 说明当前F-QUEUE队列中并没有等待Fializer线程执行finalizer方法的对象。
-dump:< dump-options >
-dump: 以hprof二进制格式将Java堆信息输出到文件内,该文件可以用MAT、VisualVM或jhat等工具查看;
dump-options选项:
live 只输出活着的对象;可选,不指定是,则输出堆中所有对象
format=b 指定输出格式为二进制
file=< file> 指定文件名及文件存储位置,例如:jmap -dump:live,format=b,file=D:\heap.bin < pid>