java调试-jmap命令

jmap命令使用

jmap是一个多功能的命令。它可以生成java程序的堆dump文件,也可以查看堆内对象实例的统计信息,查看ClassLoader的信息以及Finalizer队列。

常用用法

  • 导出对象统计信息

下面的命令生成PID为24205的java成粗的对象的统计信息,并输出到out.txt文件中:

1
nalideMacBook-Pro-4:~ nali$ jmap -histo 24205 > /Users/nali/out.txt

生成的文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
num #instances #bytes class name
----------------------------------------------
1: 962520 23100480 java.lang.Long
2: 481260 19250400 com.ximalaya.test.Ip
3: 962779 15404464 java.lang.Integer
4: 481300 15401600 java.util.HashMap$Entry
5: 6362 9556232 [I
6: 19 7341744 [Ljava.util.HashMap$Entry;
7: 319 2174856 [Ljava.lang.Object;
8: 6060 781024 <methodKlass>
9: 6060 692032 <constMethodKlass>
10: 415 486208 <constantPoolKlass>
11: 378 296288 <constantPoolCacheKlass>
12: 415 285880 <instanceKlassKlass>
13: 1358 156272 [C
14: 573 94240 [B
15: 125 52928 <methodDataKlass>
16: 478 46680 java.lang.Class
17: 704 45152 [[I
18: 631 38168 [S
19: 1334 32016 java.lang.String
20: 46 25024 <objArrayKlassKlass>
21: 101 6464 java.net.URL
22: 79 5688 java.lang.reflect.Field
23: 8 4352 <typeArrayKlassKlass>
24: 96 3840 java.util.LinkedHashMap$Entry
25: 119 3808 java.util.Hashtable$Entry
26: 94 3760 java.lang.ref.SoftReference
27: 91 2912 java.util.concurrent.ConcurrentHashMap$HashEntry
28: 61 2432 [Ljava.lang.String;

从文件中可以看到,统计信息显示了内存中实例的数量和合计。

  • 导出程序堆快照

下面的命令导出PID为24205的java程序当前的堆快照:

1
2
3
nalideMacBook-Pro-4:~ nali$ jmap -dump:format=b,file=dump.bin 24205
Dumping heap to /Users/nali/dump.bin ...
Heap dump file created

该命令成功地将运用程序的当前的堆快照导出到了dump.bin文件,之后可以使用Visual VM,MAT等工具分析对快照文件。

  • 查看java 堆(heap)使用情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
nalideMacBook-Pro-4:~ nali$ jmap -heap 30192
Attaching to process ID 30192, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.79-b02
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration: //堆内存初始化配置
//对应jvm启动参数-XX:MinHeapFreeRatio设置JVM堆最小空闲比率
MinHeapFreeRatio = 0
//对应jvm启动参数 -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率
MaxHeapFreeRatio = 100
//对应jvm启动参数-XX:MaxHeapSize=设置JVM堆的最大大小
MaxHeapSize = 2147483648 (2048.0MB)
//对应jvm启动参数-XX:NewSize=设置JVM堆的‘新生代’的默认大小
NewSize = 1310720 (1.25MB)
//对应jvm启动参数-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
MaxNewSize = 17592186044415 MB
//对应jvm启动参数-XX:OldSize=<value>:设置JVM堆的‘老生代’的大小
OldSize = 5439488 (5.1875MB)
//对应jvm启动参数-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
NewRatio = 2
//对应jvm启动参数-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值
SurvivorRatio = 8
//对应jvm启动参数-XX:PermSize=<value>:设置JVM堆的‘永生代’的初始大小
PermSize = 21757952 (20.75MB)
//对应jvm启动参数-XX:MaxPermSize=<value>:设置JVM堆的‘永生代’的最大大小
MaxPermSize = 85983232 (82.0MB)
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space: //Eden区内存分布
//Eden区总容量
capacity = 136839168 (130.5MB)
//Eden区已使用
used = 62349088 (59.460723876953125MB)
//Eden区剩余容量
free = 74490080 (71.03927612304688MB)
//Eden区使用比率
45.56377308578784% used
From Space: //其中一个Survivor区的内存分布
capacity = 17301504 (16.5MB)
used = 17299704 (16.49828338623047MB)
free = 1800 (0.00171661376953125MB)
99.98959628018466% used
To Space: //另一个Survivor区的内存分布
capacity = 27262976 (26.0MB)
used = 0 (0.0MB)
free = 27262976 (26.0MB)
0.0% used
PS Old Generation //当前的Old区内存分布
capacity = 89128960 (85.0MB)
used = 35824408 (34.164817810058594MB)
free = 53304552 (50.835182189941406MB)
40.19390330595129% used
PS Perm Generation //当前的 “永生代” 内存分布
capacity = 22020096 (21.0MB)
used = 2801672 (2.6718826293945312MB)
free = 19218424 (18.32811737060547MB)
12.723250616164435% used
734 interned Strings occupying 47744 bytes.

热评文章