0%

jmap命令

使用的JDK版本为jdk1.7.0_79。

一、常用用法

1
jmap [-F] [-dump:format=b,file=FILENAME|-heap|-histo|-permstat] 进程ID

二、含义

“进程ID”是指JVM进程ID,以上常用用法的含义是:打印或者保存相应JVM进程的Java堆信息。

三、选项

假设现在运行有一个进程ID为3937的JVM进程,以该JVM进程为实验对象,对“选项”进行说明。

3.1、“-F”

“-F”选项可与接下来的几个选项结合使用,不能单独使用。使用“-F”选项后,会开启“HotSpot Serviceability Agent”机制,这使得JVM进程收到一个“SIGSTOP”信号而被“Hung”,只有当请求的信息获取完成后,该JVM进程才会收到一个“SIGCONT”信号而重新运行。[3][4][5]

3.2、“-dump:format=b,file=FILENAME”

保存Java堆堆转存快照到文件“FILENAME”,供后续进行分析,堆转存快照的格式为“hprof二进制格式”。

3.3、“-heap”

打印Java堆的如下信息:使用的垃圾收集器,Java堆相关的几个虚拟机参数值,Java堆不同内存区域的使用情况,“字符串常量池”使用情况等。注意跟“jstat”命令的统计信息有所重叠。
执行jmap -heap 3937命令,得到如下所示结果:

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
Attaching to process ID 3937, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 24.79-b02

using thread-local object allocation.
Mark Sweep Compact GC

Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 536870912 (512.0MB)
NewSize = 1048576 (1.0MB)
MaxNewSize = 4294901760 (4095.9375MB)
OldSize = 4194304 (4.0MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 12582912 (12.0MB)
MaxPermSize = 67108864 (64.0MB)
G1HeapRegionSize = 0 (0.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 7471104 (7.125MB)
used = 790304 (0.753692626953125MB)
free = 6680800 (6.371307373046875MB)
10.578142132675438% used
Eden Space:
capacity = 6684672 (6.375MB)
used = 790304 (0.753692626953125MB)
free = 5894368 (5.621307373046875MB)
11.822629442401961% used
From Space:
capacity = 786432 (0.75MB)
used = 0 (0.0MB)
free = 786432 (0.75MB)
0.0% used
To Space:
capacity = 786432 (0.75MB)
used = 0 (0.0MB)
free = 786432 (0.75MB)
0.0% used
tenured generation:
capacity = 16429056 (15.66796875MB)
used = 7500296 (7.152839660644531MB)
free = 8928760 (8.515129089355469MB)
45.65262909810521% used
Perm Generation:
capacity = 12582912 (12.0MB)
used = 9098864 (8.677352905273438MB)
free = 3484048 (3.3226470947265625MB)
72.31127421061198% used

4104 interned Strings occupying 306480 bytes.

3.4、“-histo”

打印每个类的统计信息:类的实例数量,类的实例字节数,类的全限定名称等等。
执行jmap -histo 3937命令,得到如下所示结果(限于篇幅,只截取了第一页结果):

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
num     #instances         #bytes  class name
----------------------------------------------
1: 26723 3048040 <constMethodKlass>
2: 4967 2283080 [B
3: 11530 2245392 [C
4: 26723 1927712 <methodKlass>
5: 322 1648520 [I
6: 2768 1435312 <constantPoolKlass>
7: 2768 1051720 <instanceKlassKlass>
8: 2118 783680 <constantPoolCacheKlass>
9: 9216 368640 org.apache.lucene.index.FreqProxTermsWriter$PostingList
10: 10801 259224 java.lang.String
11: 2959 254872 java.lang.Class
12: 4057 198384 [S
13: 4340 193960 [[I
14: 1802 160184 [Ljava.util.HashMap$Entry;
15: 3964 126848 java.util.LinkedHashMap$Entry
16: 288 122976 [Lorg.apache.lucene.index.RawPostingList;
17: 1798 108408 [Ljava.lang.Object;
18: 4136 99264 java.util.concurrent.ConcurrentHashMap$HashEntry
19: 3254 78096 java.util.HashMap$Entry
20: 1014 56784 java.util.LinkedHashMap
21: 1133 54384 java.util.HashMap
22: 174 54288 <objArrayKlassKlass>
23: 190 39152 [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
24: 456 36480 java.lang.reflect.Method
25: 550 35200 java.lang.reflect.Field
26: 4059 32472 java.lang.Object
27: 1201 28824 java.util.ArrayList
28: 1079 25896 com.google.inject.TypeLiteral
29: 366 23424 org.apache.lucene.index.SegmentInfo
30: 290 18560 java.lang.reflect.Constructor
31: 622 14928 com.google.inject.Key
32: 586 14064 java.util.Hashtable$Entry
33: 437 13984 java.lang.ref.SoftReference
34: 545 13080 com.google.common.collect.CustomConcurrentHashMap$StrongEntry
35: 144 12672 org.apache.lucene.index.TermsHashPerField
36: 513 12312 sun.reflect.annotation.AnnotationInvocationHandler
37: 204 9792 java.lang.Package
38: 96 9216 java.io.ObjectStreamClass
39: 383 9192 com.google.inject.spi.Dependency
40: 555 8880 com.google.common.collect.CustomConcurrentHashMap$StrongValueReference
41: 152 8512 java.net.URL
42: 210 8400 com.google.inject.internal.ProviderInstanceBindingImpl
43: 255 8160 java.lang.ref.Finalizer
44: 507 8112 java.lang.Integer

3.5、“-permstat”

打印类加载器信息:类加载器名称,类加载器加载的类数量,类加载器加载的类字节数,父“类加载器”名称,是否存活标志,类加载器类型等等。
执行jmap -permstat 3937命令,得到如下所示结果(限于篇幅,只截取了第一页结果):

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
Attaching to process ID 3937, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 24.79-b02
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness............................................................................done.
class_loader classes bytes parent_loader alive? type

<bootstrap> 1143 4158120 null live <internal>
0x9b343750 0 0 0x9b2c85c0 live java/util/ResourceBundle$RBClassLoader@0xb0bdf870
0x9b69c608 1 1752 0x9b2c85c0 dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b3a17d0 0 0 0x9b2c85c0 live org/sonatype/guice/plexus/scanners/CloningClassLoader@0xb0d62e30
0x9b744a98 1 1752 0x9b2c85c0 dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b2c9490 1 1768 null dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b2c93e8 1 1768 null dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b2c85c0 1827 5868816 0x9b2c9440 live sun/misc/Launcher$AppClassLoader@0xb0957068
0x9b5d9d70 1 1752 0x9b2c85c0 dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b995630 1 1768 null dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b744a60 1 1864 0x9b2c85c0 dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b2c94e8 1 1768 null dead sun/reflect/DelegatingClassLoader@0xb0835250
0x9b2c9440 8 40640 null live sun/misc/Launcher$ExtClassLoader@0xb091e048
0x9b339138 1 16760 0x9b2c85c0 live org/codehaus/plexus/classworlds/realm/ClassRealm@0xb0b389f0

total = 14 2987 10098528 N/A alive=6, dead=8 N/A

参考文献: [1]http://docs.oracle.com/javase/7/docs/technotes/tools/share/jmap.html [2]man jmap [3]http://stackoverflow.com/questions/26140182/running-jmap-getting-unable-to-open-socket-file [4]https://major.io/2009/06/15/two-great-signals-sigstop-and-sigcont/ [5]http://velocity.oreilly.com.cn/2013/ppts/java_web_serviceability_agent.pdf
您的支持将鼓励我继续分享!