利用jamp和jhat查看系统对象分配情况

41 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1. jamp

root@node131:~/bes_seeyon_logs# jmap -heap 32051
Attaching to process ID 32051, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.71-b00

using thread-local object allocation.
Garbage-First (G1) GC with 8 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 16777216000 (16000.0MB)
   NewSize                  = 1363144 (1.2999954223632812MB)
   MaxNewSize               = 5591007232 (5332.0MB)
   OldSize                  = 5452592 (5.1999969482421875MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 4194304000 (4000.0MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 4194304000 (4000.0MB)
   G1HeapRegionSize         = 4194304 (4.0MB)

Heap Usage:
Exception in thread "main" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sun.tools.jmap.JMap.runTool(JMap.java:201)
	at sun.tools.jmap.JMap.main(JMap.java:130)
Caused by: java.lang.RuntimeException: unknown CollectedHeap type : class sun.jvm.hotspot.gc_interface.CollectedHeap
	at sun.jvm.hotspot.tools.HeapSummary.run(HeapSummary.java:144)
	at sun.jvm.hotspot.tools.Tool.startInternal(Tool.java:260)
	at sun.jvm.hotspot.tools.Tool.start(Tool.java:223)
	at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
	at sun.jvm.hotspot.tools.HeapSummary.main(HeapSummary.java:49)
	... 6 more

它可以打印出 Eden区总容量 、已使用的容量、剩余的空间容量、两个Survivor区的总容量、已使用的容量和剩余的空间容量、老年代的总容量、已使用的容量和剩余的容量。但是 一般不用jmap命令,原因有2个 :1 没有jstat 显示的全 2 没有GC相关统计。

# 该命令了解当前jvm中对象占用情况,它会按照对象占用空间大小降序排列,这样就可以快速了解到那个对象占用了大量内存
root@node131:~/bes_seeyon_logs# jmap -histo 32051 
num     #instances         #bytes  class name
----------------------------------------------
   1:      24122377     2122532560  [C
   2:       5169030      782332368  [B
   3:        467896      338246792  [I
   4:      13109748      314633952  java.lang.String
   5:       5465828      131179872  java.lang.StringBuilder
   6:       2668662      106746480  java.io.ObjectStreamField
   7:       2201879       97641048  [Ljava.lang.Object;
   8:        938397       82578936  java.lang.reflect.Method
   9:        607426       63172304  java.io.ObjectStreamClass
  10:       1703416       54509312  java.util.concurrent.locks.AbstractQueuedSynchronizer$Node
  11:       1272133       40708256  java.util.HashMap$Node
  12:       1288689       30928536  java.lang.Long
  13:       1059702       25432848  java.util.ArrayList
  14:        458851       24076192  [Ljava.util.HashMap$Node;
  15:        475831       22839888  java.util.HashMap
  16:        434167       17366680  java.util.TreeMap$Entry
  17:        511656       16372992  java.util.concurrent.ConcurrentHashMap$Node
  18:        507610       16243520  java.io.File
  19:        500892       16028544  java.lang.ref.WeakReference
  20:        249458       15965312  java.io.ObjectInputStream
  21:        270501       15800704  [Ljava.io.ObjectInputStream$HandleTable$HandleList;
  22:        282202       15316888  [Ljava.io.ObjectStreamField;
  23:        354535       14601512  [Ljava.lang.String;
  24:        260206       14571536  java.io.ObjectInputStream$BlockDataInputStream
  ....

2. jamp 生成当前堆内存快照

# 使用jmap 命令生成个堆内存快照到一个文件当中,因dump.hprof是二进制文件,不可直接打开
root@node131:~/bes_seeyon_logs# jmap -dump:live,format=b,file=dump.hprof 32051
Dumping heap to /data/BES952/Node/10.XX.XX.XX/instances/ins13/logs/dump.hprof ...
Heap dump file created
root@node131:~/bes_seeyon_logs# sz dump.hprof 

3. 利用jhat 在浏览器中分析堆转出快照

C:\Users\test\Desktop>jhat -port 7000  dump.hprof
Reading from dump.hprof...
Dump file created Fri Sep 24 17:17:07 CST 2021
Snapshot read, resolving...
Resolving 18466626 objects...
Chasing references, expect 3693 dots.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

由于我的 dump.hprof 文件比较大,所以分析的时间会长一些

4. jhat的用法

C:\Users\test>jhat -h
Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>

        -J<flag>          Pass <flag> directly to the runtime system. For
                          example, -J-mx512m to use a maximum heap size of 512MB
        -stack false:     Turn off tracking object allocation call stack.
        -refs false:      Turn off tracking of references to objects
        -port <port>:     Set the port for the HTTP server.  Defaults to 7000
        -exclude <file>:  Specify a file that lists data members that should
                          be excluded from the reachableFrom query.
        -baseline <file>: Specify a baseline object dump.  Objects in
                          both heap dumps with the same ID and same class will
                          be marked as not being "new".
        -debug <int>:     Set debug level.
                            0:  No debug output
                            1:  Debug hprof file parsing
                            2:  Debug hprof file parsing, no server
        -version          Report version number
        -h|-help          Print this help and exit
        <file>            The file to read

For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".

All boolean options default to "true"

从上边的默认参数可以看到jhat的默认端口是7000