[超级链接:JVM性能调优工具学习记录-序章]
本章主要学习JVM对象内存监测工具jmap和jhat
。
1.用途
jmap
:打印JVM中对象的统计信息,包括内存占用、实例个数、对象类型等。
jhat
:对Heap进行离线分析,并以Html页面显示结果的工具。
2.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
J<flag>
:设置JVM参数。h | help
:帮助。F
:在pid的情况下强制使用-dump或者-histo参数. 在这个模式下,live子参数无效.heap
:打印堆统计信息。
C:\Users\hanchao>jmap -heap 7484
Attaching to process ID 7484, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.131-b11
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 2126512128 (2028.0MB)
NewSize = 44564480 (42.5MB)
MaxNewSize = 708837376 (676.0MB)
OldSize = 89653248 (85.5MB)
...
histo
:打印堆中的Java对象直方图,包括:序号、实例个数、内存大小、类名。
C:\Users\hanchao>jmap -histo 7484
num #instances #bytes class name
----------------------------------------------
1: 308 1063632 [I
2: 11594 1036064 [C
3: 8319 199656 java.lang.String
4: 3508 177080 [Ljava.lang.String;
5: 448 150152 [B
6: 991 113016 java.lang.Class
7: 1326 65112 [Ljava.lang.Object;
histo:live
:与histo
类似,只不过只打印存活的对象。
C:\Users\hanchao>jmap -histo:live 7484
num #instances #bytes class name
----------------------------------------------
1: 6510 556792 [C
2: 6365 152760 java.lang.String
3: 447 150048 [B
4: 991 113016 java.lang.Class
5: 810 56856 [Ljava.lang.Object;
6: 835 33400 java.util.TreeMap$Entry
7: 817 26144 java.util.HashMap$Node
clstats
:打印ClassLoader统计信息。
C:\Users\hanchao>jmap -clstats 7484
Attaching to process ID 7484, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.131-b11
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness............liveness analysis may be inaccurate ...
class_loader classes bytes parent_loader alive? type
<bootstrap> 807 1472448 null live <internal>
0x0000000081402ef0 141 323773 0x0000000081402f50 live sun/misc/Launcher$AppClassLoader@0x000000010000f6a0
0x0000000081402f50 8 15664 null live sun/misc/Launcher$ExtClassLoader@0x000000010000fa48
0x00000000814d0ac0 0 0 0x0000000081402ef0 dead java/util/ResourceBundle$RBClassLoader@0x00000001000875f0
total = 4 956 1811885 N/A alive=3, dead=1 N/A
finalizerinfo
:打印等待回收的对象信息。
C:\Users\hanchao>jmap -finalizerinfo 7484
Attaching to process ID 7484, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.131-b11
Number of objects pending for finalization: 0
dump:live,format=b,file=dump.bin
:导出堆信息,要求只导出存活的对象,以二进制格式,导出文件名为dump.bin。
C:\Users\hanchao>jmap -dump:live,format=b,file=dump2018.bin 7484
Dumping heap to C:\Users\hanchao\dump2018.bin ...
Heap dump file created
3.分析过程
- 通过
jmap
或jcmd
导出heap dump至文件dump.bin。 通过
jhat dump.bin
分析文件。C:\Users\hanchao>jhat dump.bin Reading from dump.bin... Dump file created Sun May 06 23:39:09 CST 2018 Snapshot read, resolving... Resolving 175671 objects... Chasing references, expect 35 dots................................... Eliminating duplicate references................................... Snapshot resolved. Started HTTP server on port 7000 Server is ready.
- 在浏览器访问http://localhost:7000/看分析结果。