JVM性能调优工具05-jmap和jhat-JVM对象内存监测工具

[超级链接:JVM性能调优工具学习记录-序章]


本章主要学习JVM对象内存监测工具jmap和jhat。

1.用途

jmap:打印JVM中对象的统计信息,包括内存占用、实例个数、对象类型等。

jhat:对Heap进行离线分析,并以Html页面显示结果的工具。

2.jmap用法

1Usage: 2 jmap [option] <pid> 3 (to connect to running process) 4 jmap [option] <executable <core> 5 (to connect to a core file) 6 jmap [option] [server_id@]<remote server IP or hostname> 7 (to connect to remote debug server) 8 9where <option> is one of: 10 <none> to print same info as Solaris pmap 11 -heap to print java heap summary 12 -histo[:live] to print histogram of java object heap; if the "live" 13 suboption is specified, only count live objects 14 -clstats to print class loader statistics 15 -finalizerinfo to print information on objects awaiting finalization 16 -dump:<dump-options> to dump java heap in hprof binary format 17 dump-options: 18 live dump only live objects; if not specified, 19 all objects in the heap are dumped. 20 format=b binary format 21 file=<file> dump heap to <file> 22 Example: jmap -dump:live,format=b,file=heap.bin <pid> 23 -F force. Use with -dump:<dump-options> <pid> or -histo 24 to force a heap dump or histogram when <pid> does not 25 respond. The "live" suboption is not supported 26 in this mode. 27 -h | -help to print this help message 28 -J<flag> to pass <flag> directly to the runtime system 29
  • J<flag>:设置JVM参数。

  • h | help:帮助。

  • F:在pid的情况下强制使用-dump或者-histo参数. 在这个模式下,live子参数无效.

  • heap:打印堆统计信息。

1C:\Users\hanchao>jmap -heap 7484 2Attaching to process ID 7484, please wait... 3Debugger attached successfully. 4Server compiler detected. 5JVM version is 25.131-b11 6 7using thread-local object allocation. 8Parallel GC with 4 thread(s) 9 10Heap Configuration: 11 MinHeapFreeRatio = 0 12 MaxHeapFreeRatio = 100 13 MaxHeapSize = 2126512128 (2028.0MB) 14 NewSize = 44564480 (42.5MB) 15 MaxNewSize = 708837376 (676.0MB) 16 OldSize = 89653248 (85.5MB) 17... 18
  • histo:打印堆中的Java对象直方图,包括:序号、实例个数、内存大小、类名。

1C:\Users\hanchao>jmap -histo 7484 2 3 num #instances #bytes class name 4---------------------------------------------- 5 1: 308 1063632 [I 6 2: 11594 1036064 [C 7 3: 8319 199656 java.lang.String 8 4: 3508 177080 [Ljava.lang.String; 9 5: 448 150152 [B 10 6: 991 113016 java.lang.Class 11 7: 1326 65112 [Ljava.lang.Object; 12
  • histo:live:与histo类似,只不过只打印存活的对象。

1C:\Users\hanchao>jmap -histo:live 7484 2 3 num #instances #bytes class name 4---------------------------------------------- 5 1: 6510 556792 [C 6 2: 6365 152760 java.lang.String 7 3: 447 150048 [B 8 4: 991 113016 java.lang.Class 9 5: 810 56856 [Ljava.lang.Object; 10 6: 835 33400 java.util.TreeMap$Entry 11 7: 817 26144 java.util.HashMap$Node 12
  • clstats:打印ClassLoader统计信息。

1C:\Users\hanchao>jmap -clstats 7484 2Attaching to process ID 7484, please wait... 3Debugger attached successfully. 4Server compiler detected. 5JVM version is 25.131-b11 6finding class loader instances ..done. 7computing per loader stat ..done. 8please wait.. computing liveness............liveness analysis may be inaccurate ... 9class_loader classes bytes parent_loader alive? type 10 11<bootstrap> 807 1472448 null live <internal> 120x0000000081402ef0 141 323773 0x0000000081402f50 live sun/misc/Launcher$AppClassLoader@0x000000010000f6a0 130x0000000081402f50 8 15664 null live sun/misc/Launcher$ExtClassLoader@0x000000010000fa48 140x00000000814d0ac0 0 0 0x0000000081402ef0 dead java/util/ResourceBundle$RBClassLoader@0x00000001000875f0 15 16total = 4 956 1811885 N/A alive=3, dead=1 N/A 17
  • finalizerinfo:打印等待回收的对象信息。

1C:\Users\hanchao>jmap -finalizerinfo 7484 2Attaching to process ID 7484, please wait... 3Debugger attached successfully. 4Server compiler detected. 5JVM version is 25.131-b11 6Number of objects pending for finalization: 0 7
  • dump:live,format=b,file=dump.bin:导出堆信息,要求只导出存活的对象,以二进制格式,导出文件名为dump.bin。

1C:\Users\hanchao>jmap -dump:live,format=b,file=dump2018.bin 7484 2Dumping heap to C:\Users\hanchao\dump2018.bin ... 3Heap dump file created 4

3.分析过程

  1. 通过jmap或jcmd导出heap dump至文件dump.bin。

通过jhat dump.bin分析文件。

1C:\Users\hanchao>jhat dump.bin 2Reading from dump.bin... 3Dump file created Sun May 06 23:39:09 CST 2018 4Snapshot read, resolving... 5Resolving 175671 objects... 6Chasing references, expect 35 dots................................... 7Eliminating duplicate references................................... 8Snapshot resolved. 9Started HTTP server on port 7000 10Server is ready. 11 12
  1. 在浏览器访问http://localhost:7000/看分析结果。

代码交流 2021