JVM工具系列第4篇-jmap:java内存映射工具

star2017 1年前 ⋅ 281 阅读

目录

jmap

jdk安装后会自带一些小工具,jmap命令(Memory Map for Java)是其中之一。主要用于打印指定Java进程(或核心文件、远程调试服务器)的共享对象内存映射或堆内存细节。

jmap命令可以获得运行中的jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等。可以使用jmap生成Heap Dump。

如果不想使用jmap命令,要想获取Java堆转储快照还有一些比较“暴力”的手段:譬如在前面用过的 -XX:+HeapDumpOnOutOfMemoryError参数,可以让虚拟机在OOM异常出现之后自动生成dump文件,通过-XX:+HeapDumpOnCtrlBreak参数可以使用[ctrl]+[Break]键让虚拟机生成dump文件,又或者在Linux系统下通过Kill -3 命令发送进程退出信息“恐吓”一下虚拟机,也能拿到dump文件。

jmap的作用并不仅仅是为了获取dump文件,他还可以查询finalize执行队列,java堆和永久代的详细信息,如空间使用率、当前用的是哪种收集器等。

jmap命令格式

  1. [root@ady01 ~]# jmap
  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

主要选项:

选项 作用
-dump 生成java堆转储快照,格式为:-dump:[live,]format=b,file=<filename>,其中live子参数说明是否只dump出存活对象
-finalizerinfo 显示在F-Queue中等待Finalizer线程执行finalize方法的对象,只在linux/solaris平台下有效
-heap 显示堆详细信息,如使用哪种回收期、参数配置、分带状况等,只在linux/solaris平台下有效
-histo 显示堆中对象统计信息,包括类、实例数量和合计容量
-permstat 以ClassLoader为统计口径显示永久代内存状况,只在linux/solaris平台下有效
-F 当虚拟机进程对-dump选项没有响应时,可以使用这个选项强制生成dump快照,只在linux/solaris平台下有效

jmap -dump:生成java堆转储快照

生成java对转存快照,格式:jmap -dump:[live,]format=b,file=文件名 <pid>

  1. C:\Users\Think>jmap -dump:live,format=b,file=D:/1.hprof 24956
  2. Dumping heap to D:\1.hprof ...
  3. Heap dump file created

可以使用jdk提供的jvisualvm.exe查看hprof文件
file

jmap -heap:显示堆详细信息

显示堆详细信息

命令格式:jmap -heap <pid>

  1. [root@ady01 ~]# jmap -heap 25867
  2. Attaching to process ID 25867, please wait...
  3. Debugger attached successfully.
  4. Server compiler detected.
  5. JVM version is 25.181-b13
  6. using thread-local object allocation.
  7. Parallel GC with 2 thread(s)
  8. Heap Configuration:
  9. MinHeapFreeRatio = 0
  10. MaxHeapFreeRatio = 100
  11. MaxHeapSize = 4164943872 (3972.0MB)
  12. NewSize = 87031808 (83.0MB)
  13. MaxNewSize = 1388314624 (1324.0MB)
  14. OldSize = 175112192 (167.0MB)
  15. NewRatio = 2
  16. SurvivorRatio = 8
  17. MetaspaceSize = 21807104 (20.796875MB)
  18. CompressedClassSpaceSize = 1073741824 (1024.0MB)
  19. MaxMetaspaceSize = 17592186044415 MB
  20. G1HeapRegionSize = 0 (0.0MB)
  21. Heap Usage:
  22. PS Young Generation
  23. Eden Space:
  24. capacity = 47710208 (45.5MB)
  25. used = 2632072 (2.5101394653320312MB)
  26. free = 45078136 (42.98986053466797MB)
  27. 5.516790033696772% used
  28. From Space:
  29. capacity = 1048576 (1.0MB)
  30. used = 770128 (0.7344512939453125MB)
  31. free = 278448 (0.2655487060546875MB)
  32. 73.44512939453125% used
  33. To Space:
  34. capacity = 524288 (0.5MB)
  35. used = 0 (0.0MB)
  36. free = 524288 (0.5MB)
  37. 0.0% used
  38. PS Old Generation
  39. capacity = 220200960 (210.0MB)
  40. used = 98595728 (94.02821350097656MB)
  41. free = 121605232 (115.97178649902344MB)
  42. 44.77533976236979% used
  43. 38803 interned Strings occupying 4463232 bytes.

jmap -histo:显示堆中对象统计信息

显示堆中对象统计信息,包括类、实例数量和合计容量

命令格式:jmap -histo[:live] <pid>

  1. C:\Users\Think>jmap -histo 28252
  2. num #instances #bytes class name
  3. ----------------------------------------------
  4. 1: 309006 16963968 [C
  5. 2: 1081 7275840 [I
  6. 3: 41164 3156952 [B
  7. 4: 90125 2163000 java.lang.String
  8. 5: 21000 672000 java.util.UUID
  9. 6: 21000 336000 com.jvm.test8.Test8$User
  10. 7: 21000 336000 com.jvm.test8.Test8$User$UserBuilder
  11. 8: 799 300072 [Ljava.lang.Object;
  12. 9: 7557 181368 java.lang.StringBuilder
  13. 10: 772 87704 java.lang.Class
  14. 11: 1026 65664 sun.nio.fs.WindowsFileAttributes
  15. 12: 1026 49248 sun.nio.fs.WindowsPath$WindowsPathWithAttributes
  16. 13: 837 33480 java.util.TreeMap$Entry
  17. 14: 1032 33024 java.lang.ref.WeakReference
  18. 15: 775 31000 sun.nio.fs.WindowsPath
  19. 16: 1028 24672 sun.nio.fs.WindowsPathParser$Result
  20. 17: 424 13568 java.io.File
  21. 18: 168 12096 java.lang.reflect.Field
  22. 19: 299 11960 java.util.LinkedHashMap$Entry
  23. 20: 323 11200 [Ljava.lang.String;
  24. 21: 173 11072 java.net.URL
  25. 22: 341 10912 sun.misc.FDBigInteger
  26. 23: 312 9984 java.util.Hashtable$Entry
  27. 24: 66 8992 [Ljava.util.HashMap$Node;
  28. 25: 267 8544 java.util.HashMap$Node
  29. 26: 195 7800 java.lang.ref.Finalizer
  30. 27: 264 6336 java.lang.StringBuffer
  31. 28: 121 4840 java.lang.ref.SoftReference
  32. 29: 29 4816 [Ljava.util.Hashtable$Entry;
  33. 30: 50 4800 java.util.jar.JarFile$JarFileEntry
  34. 31: 105 4552 [[C
  35. 32: 53 4240 [Ljava.util.WeakHashMap$Entry;
  36. 33: 74 4144 sun.misc.URLClassPath$JarLoader
  37. 34: 258 4128 java.lang.Integer
  38. 35: 50 4000 java.util.zip.ZipEntry
  39. 36: 79 3792 java.net.NetworkInterface
  40. 37: 150 3600 java.net.Parts
  41. 38: 111 3552 java.util.concurrent.ConcurrentHashMap$Node
  42. 39: 134 3216 java.security.Provider$ServiceKey
  43. 40: 50 3200 java.util.jar.JarFile
  44. 41: 55 3080 sun.nio.cs.UTF_8$Encoder
  45. 42: 8 3008 java.lang.Thread
  46. 43: 62 2976 java.util.HashMap
  47. 44: 51 2856 java.util.zip.ZipFile$ZipFileInputStream
  48. 45: 114 2736 java.io.ExpiringCache$Entry
  49. 46: 53 2544 java.util.WeakHashMap
  50. 47: 30 2400 java.lang.reflect.Constructor
  51. 48: 56 2240 java.util.WeakHashMap$Entry
  52. 49: 39 2184 java.util.zip.ZipFile$ZipFileInflaterInputStream

最新资料

更多内容请访问:IT源点

全部评论: 0

    我有话说: