jmap的使用以及内存溢出分析
jmap(java内存映像工具)
jmap(Memory Map for Java)命令用于生成堆转储快照(一般称为heapdump或dump文件)。还有几种方式获取dump文件:使用JVM参数选项-XX:+HeapDumpOnOutOfMemoryError参数,可以让虚拟机在OOM异常出现之后自动生成dump文件,通过-XX:HeapDumpPath=path 设置dump文件路径(有时候dump文件比较大的时候可能无法自动导出,这时候就需要使用jmap -dump手动导出了);通过-XX.+HeapDumpOnCtrlBreak参数则可以使用[Ctrl]+[Break]键让虚拟机生成dump文件;或者在Linux系统下通过Kill -3命令发送进程退出信号,也能拿到dump文件。
jmap的作用并不仅仅是为了获取dump文件,它还可以查询finalize执行队列、Java堆和永久代的详细信息,如空间使用率、当前用的是哪种收集器等。和jinfo命令一样,jmap有不少功能在Windows平台下都是受限的,除了生成dump文件的-dump选项和用于查看每个类的实例、空间占用统计的-histo选项在所有操作系统都提供之外,其余选项都只能在Linux/Solans下使用。官方文档地址
1.1、查看内存使用情况
[root@localhost /]# jmap -heap 9906
Attaching to process ID 9906, please wait…
Debugger attached successfully.
Server compiler detected.
JVM version is 25.221-b11
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 989855744 (944.0MB)
NewSize = 20971520 (20.0MB)
MaxNewSize = 329908224 (314.625MB)
OldSize = 41943040 (40.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 = 18939904 (18.0625MB)
used = 5314648 (5.068443298339844MB)
free = 13625256 (12.994056701660156MB)
28.06058573475346% used
Eden Space:
capacity = 16842752 (16.0625MB)
used = 4780064 (4.558624267578125MB)
free = 12062688 (11.503875732421875MB)
28.38054018725681% used
From Space:
capacity = 2097152 (2.0MB)
used = 534584 (0.5098190307617188MB)
free = 1562568 (1.4901809692382812MB)
25.490951538085938% used
To Space:
capacity = 2097152 (2.0MB)
used = 0 (0.0MB)
free = 2097152 (2.0MB)
0.0% used
tenured generation:
capacity = 41943040 (40.0MB)
used = 36145136 (34.47068786621094MB)
free = 5797904 (5.5293121337890625MB)
86.17671966552734% used
31688 interned Strings occupying 3707776 bytes.
1.2、查看内存中对象数量及大小
#查看所有对象,包括活跃以及非活跃的
jmap -histo <pid> | more
#查看活跃对象
jmap -histo:live <pid> | more
[root@localhost /]# jmap -histo:live 9906 | more
num #instances #bytes class name
----------------------------------------------
1: 72627 10929400 [C
2: 12052 2445368 [I
3: 3709 1718968 [B
4: 71294 1711056 java.lang.String
5: 14306 1258928 java.lang.reflect.Method
6: 8153 922992 java.lang.Class
7: 26390 844480 java.util.HashMap$Node
8: 23271 744672 java.util.concurrent.ConcurrentHashMap$Node
9: 10490 629792 [Ljava.lang.Object;
10: 5128 536080 [Ljava.util.HashMap$Node;
#对象说明
B byte
C char
D double
F float
I int
J long
Z boolean
[数组,如[I表示int[]
[L+类名 其他对象
1.3、将内存使用情况dump到文件中
#用法:
jmap -dump:format=b,file=dumpFileName <pid>
#示例
[root@localhost /]# jmap -dump:format=b,file=/cfile/dump.dat 9906
Dumping heap to /cfile/dump.dat …
Heap dump file created
|