一,jmap查看帮助:
[lhdop@blog ~]$ jmap --help Usage: jmap -clstats <pid> to connect to running process and print class loader statistics jmap -finalizerinfo <pid> to connect to running process and print information on objects awaiting finalization jmap -histo[:[<histo-options>]] <pid> to connect to running process and print histogram of java object heap jmap -dump:<dump-options> <pid> to connect to running process and dump java heap jmap -? -h --help to print this help message dump-options: live dump only live objects all dump all objects in the heap (default if one of "live" or "all" is not specified format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> histo-options: live count only live objects all count all objects in the heap (default if one of "live" or "all" is not specified) file=<file> dump data to <file> Example: jmap -histo:live,file=/tmp/histo.data <pid>
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,jmap的常用例子
1,显示堆中对象的统计信息
[lhdop@blog ~]$ jmap -histo:live 5426 | more No dump file specified num #instances #bytes class name (module) ------------------------------------------------------- 1: 262144 6291456 org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapper 2: 77084 5499016 [B (java.base@15) 3: 5678 4699536 [I (java.base@15) 4: 72731 1745544 java.lang.String (java.base@15) 5: 11294 1692816 [Ljava.lang.Object; (java.base@15) 6: 48063 1538016 java.util.concurrent.ConcurrentHashMap$Node (java.base@15) 7: 12598 1501528 java.lang.Class (java.base@15) 8: 15105 1329240 java.lang.reflect.Method (java.base@15) 9: 547 902144 [C (java.base@15) 10: 18089 578848 java.util.HashMap$Node (java.base@15) 11: 5975 530328 [Ljava.util.HashMap$Node; (java.base@15) 12: 12632 505280 java.util.LinkedHashMap$Entry (java.base@15) 13: 445 482320 [Ljava.util.concurrent.ConcurrentHashMap$Node; (java.base@15) 14: 25730 411680 java.lang.Object (java.base@15) 15: 6481 362936 java.util.LinkedHashMap (java.base@15) 16: 11761 266568 [Ljava.lang.Class; (java.base@15) ...
2,打印类加载器信息
[lhdop@blog ~]$ jmap -clstats 5426 ClassLoader Parent CLD* Classes ChunkSz BlockSz Type 0x00000008000a6190 0x0000000800b98840 0x00007f86240485a0 1 3072 2024 jdk.internal.reflect.DelegatingClassLoader 0x00000008000a6190 0x0000000800b98840 0x00007f8638279130 1 6144 4032 jdk.internal.reflect.DelegatingClassLoader 0x00000008000a6190 0x000000080007f230 0x00007f8628201320 1 4096 3232 jdk.internal.reflect.DelegatingClassLoader 0x00000008000a6190 0x0000000800b98840 0x00007f86b94f11a0 1 6144 3968 jdk.internal.reflect.DelegatingClassLoader 0x00000008000a6190 0x0000000800b98840 0x0000565402b9e740 1 3072 2024 jdk.internal.reflect.DelegatingClassLoader 0x00000008000a6190 0x000000080007f230 0x00007f8648278e70 1 6144 4784 jdk.internal.reflect.DelegatingClassLoader 0x00000008000a6190 0x0000000800b98840 0x00007f863000a560 1 6144 4056 jdk.internal.reflect.DelegatingClassLoader … Total = 456 12549 65325056 64231288 ChunkSz: Total size of all allocated metaspace chunks BlockSz: Total size of all allocated metaspace blocks (each chunk has several blocks)
3,打印等待终结的对象信息
[lhdop@blog ~]$ jmap -finalizerinfo 5426 No instances waiting for finalization found
4,以hprof二进制格式转储Java堆到指定filename的文件中
[lhdop@blog ~]$ jmap -dump:live,format=b,file=heap.hprof 5426 Dumping heap to /home/lhdop/heap.hprof ... Heap dump file created [60868455 bytes in 0.264 secs]
查看生成的二进制文件:
[lhdop@blog ~]$ ll heap.hprof -rw------- 1 lhdop lhdop 60868455 Mar 26 17:08 heap.hprof
说明:
dump的文件可以使用eclipse的MAT或jdk自带的jvisualvm分析内存泄露和内存溢出问题。
三,查看java版本:
[lhdop@blog tools]$ java --version java 15 2020-09-15 Java(TM) SE Runtime Environment (build 15+36-1562) Java HotSpot(TM) 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)