概述

Jmap是java简单的查看内存使用情况工具,可以对一些使用情况进行查询。
可以生成heap dump文件,也可以使用-XX:+HeapDumpOnOutOfMemoryError 参数让虚拟机出现OOM的时候自动生成dump文件。

命令格式

  • jmap [ option ] pid
  • jmap [ option ] executable core
  • jmap [ option ] [server-id@]remote-hostname-or-IP

常用命令

jmap -heap pid

说明:打印heap空间的概要信息

  1. using parallel threads in the new generation. ##新生代采用的是并行线程处理方式
  2. using thread-local object allocation.
  3. Concurrent Mark-Sweep GC ##同步并行垃圾回收
  4. Heap Configuration: ##堆配置情况
  5. MinHeapFreeRatio = 40 ##最小堆使用比例
  6. MaxHeapFreeRatio = 70 ##最大堆可用比例
  7. MaxHeapSize = 2147483648 (2048.0MB) ##最大堆空间大小
  8. NewSize = 268435456 (256.0MB) ##新生代分配大小
  9. MaxNewSize = 268435456 (256.0MB) ##最大可新生代分配大小
  10. OldSize = 5439488 (5.1875MB) ##老生代大小
  11. NewRatio = 2 ##新生代比例
  12. SurvivorRatio = 8 ##新生代与suvivor的比例
  13. PermSize = 134217728 (128.0MB) ##perm区大小
  14. MaxPermSize = 134217728 (128.0MB) ##最大可分配perm区大小
  15. ...

jmap -dump:live,format=b,file=xxx 2657

说明:此方式能够产生指定存活的对象结果
dump堆到文件,format指定输出格式,live指明是活着的对象,file指定文件名。

jmap -histo:live pid

说明:查看实例内容

  1. num #instances #bytes class name
  2. ----------------------------------------------
  3. 1: 51844 7793984 [C
  4. 2: 17515 1541320 java.lang.reflect.Method
  5. 3: 50676 1216224 java.lang.String
  6. 4: 8309 928304 java.lang.Class
  7. 5: 28187 901984 java.util.concurrent.ConcurrentHashMap$Node
  8. 6: 12532 501280 java.util.LinkedHashMap$Entry
  9. 7: 8037 454800 [Ljava.lang.Object;

注:
第一列:编号id
第二列:实例个数
第三列:所有实例大小
第四列:类名

显示前几条

命令后面加上| head -10指定展示多少条数

显示类是否加载

命令后面加上| grep className 显示类是否加载

-permstat:打印一些持久代上的内存使用状态

实例

1.打印实际线程输出结果:

  1. [nobody@aliyun-APP-170-10 logs]$ jmap -heap 11399
  2. Attaching to process ID 11399, please wait...
  3. Debugger attached successfully.
  4. Server compiler detected.
  5. JVM version is 24.65-b04
  6. using thread-local object allocation.
  7. Parallel GC with 8 thread(s)
  8. Heap Configuration:
  9. MinHeapFreeRatio = 0
  10. MaxHeapFreeRatio = 100
  11. MaxHeapSize = 536870912 (512.0MB)
  12. NewSize = 1310720 (1.25MB)
  13. MaxNewSize = 17592186044415 MB
  14. OldSize = 5439488 (5.1875MB)
  15. NewRatio = 2
  16. SurvivorRatio = 8
  17. PermSize = 21757952 (20.75MB)
  18. MaxPermSize = 134217728 (128.0MB)
  19. G1HeapRegionSize = 0 (0.0MB)
  20. Heap Usage:
  21. PS Young Generation
  22. Eden Space:
  23. capacity = 44040192 (42.0MB)
  24. used = 35082336 (33.457122802734375MB)
  25. free = 8957856 (8.542877197265625MB)
  26. 79.65981619698661% used
  27. From Space:
  28. capacity = 524288 (0.5MB)
  29. used = 327680 (0.3125MB)
  30. free = 196608 (0.1875MB)
  31. 62.5% used
  32. To Space:
  33. capacity = 524288 (0.5MB)
  34. used = 0 (0.0MB)
  35. free = 524288 (0.5MB)
  36. 0.0% used
  37. PS Old Generation
  38. capacity = 89653248 (85.5MB)
  39. used = 75464792 (71.96883392333984MB)
  40. free = 14188456 (13.531166076660156MB)
  41. 84.17407476414017% used
  42. PS Perm Generation
  43. capacity = 63963136 (61.0MB)
  44. used = 58953904 (56.22282409667969MB)
  45. free = 5009232 (4.7771759033203125MB)
  46. 92.16856409291752% used
  47. 27397 interned Strings occupying 3112776 bytes.
  1. 打印出永久带的内容:
  1. [nobody@aliyun-APP-170-10 logs]$ jmap -permstat 11399
  2. Attaching to process ID 11399, please wait...
  3. Debugger attached successfully.
  4. Server compiler detected.
  5. JVM version is 24.65-b04
  6. finding class loader instances ..done.
  7. computing per loader stat ..done.
  8. please wait.. computing liveness..........................................liveness analysis may be inaccurate ...
  9. class_loader classes bytes parent_loader alive? type
  10. <bootstrap> 3637 20196224 null live <internal>
  11. 0x00000000e3895908 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  12. 0x00000000e3661fa0 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  13. 0x00000000e230c920 0 0 0x00000000e017b5e8 dead
  14. com/alibaba/fastjson/util/ASMClassLoader@0x00000000daa4c500
  15. 0x00000000e1172830 29 368864 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  16. 0x00000000e3892a98 1 3048 0x00000000e037c0c8 dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  17. 0x00000000e2754380 45 394184 0x00000000e037c0c8 live org/apache/jasper/servlet/JasperLoader@0x00000000db147870
  18. 0x00000000e38951d8 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  19. 0x00000000e017b5e8 117 1172624 0x00000000e017b638 live sun/misc/Launcher$AppClassLoader@0x00000000d8213318
  20. 0x00000000e1fc73f0 1 6680 0x00000000e017b598 live org/apache/catalina/loader/WebappClassLoader@0x00000000d8db2618
  21. 0x00000000e1fcca80 0 0 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  22. 0x00000000e3892700 1 3048 0x00000000e037c0c8 dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  23. 0x00000000e2224558 1 6680 0x00000000e017b598 live org/apache/catalina/loader/WebappClassLoader@0x00000000d8db2618
  24. 0x00000000e22ac0c8 49 332968 0x00000000e017b598 live org/apache/catalina/loader/WebappClassLoader@0x00000000d8db2618
  25. 0x00000000e366dbf8 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  26. 0x00000000e1fd7890 0 0 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  27. 0x00000000e001c408 0 0 0x00000000e017b5e8 dead java/util/ResourceBundle$RBClassLoader@0x00000000d8408280
  28. 0x00000000e3892e30 1 3048 0x00000000e037c0c8 dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  29. 0x00000000e017b638 115 642528 null live sun/misc/Launcher$ExtClassLoader@0x00000000d81ba4f8
  30. 0x00000000e027a778 4 13776 null dead javax/management/remote/rmi/NoCallStackClassLoader@0x00000000d8757bb8
  31. 0x00000000e3895570 1 3048 0x00000000e037c0c8 dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  32. 0x00000000e1a6b7e8 85 724464 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  33. 0x00000000e1a89ce8 0 0 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  34. 0x00000000e1a811e8 0 0 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  35. 0x00000000e1fc0ab0 0 0 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  36. 0x00000000e027a840 2 32144 null dead javax/management/remote/rmi/NoCallStackClassLoader@0x00000000d8757bb8
  37. 0x00000000e3892368 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  38. 0x00000000e3662320 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  39. 0x00000000e037c0c8 4804 27974784 0x00000000e017b598 live org/apache/catalina/loader/WebappClassLoader@0x00000000d8db2618
  40. 0x00000000e3661c20 1 3048 0x00000000e037c0c8 dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  41. 0x00000000e017b598 1546 13312232 0x00000000e017b5e8 live org/apache/catalina/loader/StandardClassLoader@0x00000000d8803c68
  42. 0x00000000e36618a0 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  43. 0x00000000e3661520 1 3048 null dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  44. 0x00000000e36611a0 1 3048 0x00000000e037c0c8 dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  45. 0x00000000e3891fd0 1 3048 0x00000000e037c0c8 dead sun/reflect/DelegatingClassLoader@0x00000000d804fc00
  46. 0x00000000e1a74290 0 0 0x00000000e037c0c8 live com/alibaba/fastjson/util/ASMClassLoader@0x00000000d92261a0
  47. 0x00000000e21436b0 1 6680 0x00000000e017b598 live org/apache/catalina/loader/WebappClassLoader@0x00000000d8db2618
  48. total = 37 10451 65230552 N/A alive=18, dead=19 N/A

参考