1 概述

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

  1. [webuser@-TESTENV interaction-like]$ jmap -help
  2. Usage:
  3. jmap [option] <pid>
  4. (to connect to running process)
  5. jmap [option] <executable <core>
  6. (to connect to a core file)
  7. jmap [option] [server_id@]<remote server IP or hostname>
  8. (to connect to remote debug server)
  9. where <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

参数:

  • pid: 需要打印配置信息的进程ID。
  • heap: 显示Java堆详细信息
  • histo[:live]: 显示堆中对象的统计信息
  • clstats:打印类加载器信息
  • finalizerinfo: 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
  • dump:生成堆转储快照
  • F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
  • help:打印帮助信息
  • J指定传递给运行jmap的JVM的参数

2 常用命令

2.1 jmap -heap pid

  1. [webuser@CSDN-TESTENV interaction-like]$ jmap -heap 7585
  2. Attaching to process ID 7585, please wait...
  3. Debugger attached successfully.
  4. Server compiler detected.
  5. JVM version is 25.161-b12
  6. using thread-local object allocation.
  7. Parallel GC with 2 thread(s) //GC 方式
  8. Heap Configuration: //堆内存初始化配置
  9. MinHeapFreeRatio = 0 //对应jvm启动参数-XX:MinHeapFreeRatio设置JVM堆最小空闲比率(default 40)
  10. MaxHeapFreeRatio = 100 //对应jvm启动参数 -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率(default 70)
  11. MaxHeapSize = 2082471936 (1986.0MB) //对应jvm启动参数-XX:MaxHeapSize=设置JVM堆的最大大小
  12. NewSize = 1310720 (1.25MB)//对应jvm启动参数-XX:NewSize=设置JVM堆的‘新生代’的默认大小
  13. MaxNewSize = 17592186044415 MB//对应jvm启动参数-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
  14. OldSize = 5439488 (5.1875MB)//对应jvm启动参数-XX:OldSize=<value>:设置JVM堆的‘老生代’的大小
  15. NewRatio = 2 //对应jvm启动参数-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
  16. SurvivorRatio = 8 //对应jvm启动参数-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值
  17. PermSize = 21757952 (20.75MB) //对应jvm启动参数-XX:PermSize=<value>:设置JVM堆的‘永生代’的初始大小
  18. MaxPermSize = 85983232 (82.0MB)//对应jvm启动参数-XX:MaxPermSize=<value>:设置JVM堆的‘永生代’的最大大小
  19. G1HeapRegionSize = 0 (0.0MB)
  20. Heap Usage: //堆内存使用情况
  21. PS Young Generation
  22. Eden Space:
  23. capacity = 110624768 (105.5MB)
  24. used = 92517392 (88.23146057128906MB)
  25. free = 18107376 (17.268539428710938MB)
  26. 83.63171618131665% used
  27. From Space:
  28. capacity = 110624768 (105.5MB)
  29. used = 819200 (0.78125MB)
  30. free = 109805568 (104.71875MB)
  31. 0.740521327014218% used
  32. To Space:
  33. capacity = 110624768 (105.5MB)
  34. used = 0 (0.0MB)
  35. free = 110624768 (105.5MB)
  36. 0.0% used
  37. PS Old Generation
  38. capacity = 337641472 (322.0MB)
  39. used = 33530208 (31.976898193359375MB)
  40. free = 304111264 (290.0231018066406MB)
  41. 9.930713724645768% used
  42. 25039 interned Strings occupying 2681056 bytes.

2.2 jmap -histo:live pid

显示堆中对象的统计信息,其中包括每个Java类、对象数量、内存大小(单位:字节)、完全限定的类名。打印的虚拟机内部的类名称将会带有一个’*’前缀。如果指定了live子选项,则只计算活动的对象。

[webuser@CSDN-TESTENV root]$ jmap -histo:live 7585

 num     #instances         #bytes  class name
----------------------------------------------
   1:         74899        8341672  [C
   2:         68280        2184960  java.util.concurrent.ConcurrentHashMap$Node
   3:         17683        1950672  java.lang.Class
   4:         17245        1842640  [Ljava.lang.Object;
   5:          4289        1819152  [B
   6:         20618        1814384  java.lang.reflect.Method
   7:         74269        1782456  java.lang.String
   8:          9767        1164856  [I
   9:         46643         746288  java.lang.Object
  10:           298         712544  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  11:          6865         598600  [Ljava.util.HashMap$Node;
  12:         14097         563880  java.util.LinkedHashMap$Entry
  13:           192         497400  [J
  14:          7552         422912  java.util.LinkedHashMap
  15:         12501         400032  java.util.HashMap$Node
  16:          8184         392832  org.aspectj.weaver.reflect.ShadowMatchImpl
  17:         14789         341704  [Ljava.lang.Class;
  18:          8184         261888  org.aspectj.weaver.patterns.ExposedState
  19:          4154         199392  org.springframework.core.ResolvableType
  20:          7667         184008  java.util.ArrayList
  21:          5651         180832  java.lang.ref.WeakReference
  22:          4464         178560  java.lang.ref.Finalizer
  23:          2633         177336  [Ljava.lang.String;
....

2.3 jmap -finalizerinfo pid

打印等待回收的对象信息

[webuser@-TESTENV root]$ jmap -finalizerinfo 7585
Attaching to process ID 7585, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12
Number of objects pending for finalization: 0
# 说明当前F-QUEUE队列中并没有等待Fializer线程执行finalizer方法的对象。

2.4 jmap -dump:

描述:生成堆转储快照dump文件。以hprof二进制格式转储Java堆到指定filename的文件中。live子选项是可选的。如果指定了live子选项,堆中只有活动的对象会被转储。想要浏览heap dump,可以用MAT、VisualVM或jhat等工具查看。
这个命令执行,JVM会将整个heap的信息dump写入到一个文件,heap如果比较大的话,就会导致这个过程比较耗时,并且执行的过程中为了保证dump的信息是可靠的,所以会暂停应用, 线上系统慎用。

[webuser@CSDN-TESTENV interaction-like]$ jmap -dump:live,format=b,file=heapDump 7585
Dumping heap to /data/logs/interaction-like/heapDump ...
Heap dump file created
[webuser@CSDN-TESTENV interaction-like]$ jhat -port 5000 heapDump //通过浏览器访问,有时你dump出来的堆很大,在启动时会报堆空间不足的错误,需要指定大小
Reading from heapDump...
Dump file created Thu Apr 22 15:35:50 CST 2021
Snapshot read, resolving...
Resolving 614075 objects...
Chasing references, expect 122 dots..........................................................................................................................
Eliminating duplicate references..........................................................................................................................
Snapshot resolved.
Started HTTP server on port 5000
Server is ready.