概述
jmap 全称:Java Memory Map(Java内存映射)
jmap是java虚拟机自带的一种内存映像工具。
jmap是一个多功能的命令。它可以生成 java 程序的 dump 文件, 也可以查看堆内对象示例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。
常见的使用在解决内存溢出等问题
jmap 用法
➜ jmap Usage: jmap [option] <pid> (to connect to running process) jmap [option] <executable <core> (to connect to a core file) jmap [option] [server_id@]<remote server IP or hostname> (to connect to remote debug server) where <option> is one of: <none> to print same info as Solaris pmap -heap to print java heap summary -histo[:live] to print histogram of java object heap; if the "live" suboption is specified, only count live objects -clstats to print class loader statistics -finalizerinfo to print information on objects awaiting finalization -dump:<dump-options> to dump java heap in hprof binary format dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> -F force. Use with -dump:<dump-options> <pid> or -histo to force a heap dump or histogram when <pid> does not respond. The "live" suboption is not supported in this mode. -h | -help to print this help message -J<flag> to pass <flag> directly to the runtime system
参数:
option
:选项参数。pid
:需要打印配置信息的进程ID。executable
:产生核心dump的Java可执行文件。core
:需要打印配置信息的核心文件。server-id
:可选的唯一id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标识服务器。remote server IP or hostname
:远程调试服务器的IP地址或主机名。
option
no option
:查看进程的内存映像信息,类似 Solaris pmap 命令。heap
:显示Java堆详细信息histo[:live]
:显示堆中对象的统计信息clstats
:打印类加载器信息finalizerinfo
:显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象dump:<dump-options>
:生成堆转储快照F
: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.help
:打印帮助信息J<flag>
:指定传递给运行jmap的JVM的参数
三、示例
1、示例1:no option
命令:jmap pid
描述:查看进程的内存映像信息,类似 Solaris pmap 命令。
使用不带选项参数的jmap打印共享对象映射,将会打印目标虚拟机中加载的每个共享对象的起始地址、映射大小以及共享对象文件的路径全称。
[root@searching-search-service-7cbfd67967-lczgh /]# jmap 19 Attaching to process ID 19, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 0x0000000000400000 7K /usr/local/jdk1.8.0_161/bin/java 0x00007f895aba2000 86K /usr/lib64/libgcc_s-4.8.5-20150702.so.1 0x00007f895adb8000 251K /usr/local/jdk1.8.0_161/jre/lib/amd64/libsunec.so 0x00007f89a5683000 104K /usr/lib64/libresolv-2.17.so 0x00007f89a589c000 31K /usr/lib64/libnss_dns-2.17.so 0x00007f89a68c2000 91K /usr/local/jdk1.8.0_161/jre/lib/amd64/libnio.so 0x00007f89a6cd3000 49K /usr/local/jdk1.8.0_161/jre/lib/amd64/libmanagement.so 0x00007f89a71dd000 112K /usr/local/jdk1.8.0_161/jre/lib/amd64/libnet.so 0x00007f89f06d0000 21K /usr/local/jdk1.8.0_161/jre/lib/amd64/libdt_socket.so 0x00007f8a0c272000 125K /usr/local/jdk1.8.0_161/jre/lib/amd64/libzip.so 0x00007f8a0c48e000 60K /usr/lib64/libnss_files-2.17.so 0x00007f8a0c6a1000 14K /usr/local/jdk1.8.0_161/jre/lib/amd64/libnpt.so 0x00007f8a0c8a4000 265K /usr/local/jdk1.8.0_161/jre/lib/amd64/libjdwp.so 0x00007f8a0cadf000 48K /usr/local/jdk1.8.0_161/jre/lib/amd64/libinstrument.so 0x00007f8a0cce9000 221K /usr/local/jdk1.8.0_161/jre/lib/amd64/libjava.so 0x00007f8a0cf15000 64K /usr/local/jdk1.8.0_161/jre/lib/amd64/libverify.so 0x00007f8a0d124000 43K /usr/lib64/librt-2.17.so 0x00007f8a0d32c000 1112K /usr/lib64/libm-2.17.so 0x00007f8a0d62e000 16643K /usr/local/jdk1.8.0_161/jre/lib/amd64/server/libjvm.so 0x00007f8a0e62b000 2122K /usr/lib64/libc-2.17.so 0x00007f8a0e9f8000 19K /usr/lib64/libdl-2.17.so 0x00007f8a0ebfc000 101K /usr/local/jdk1.8.0_161/lib/amd64/jli/libjli.so 0x00007f8a0ee12000 141K /usr/lib64/libpthread-2.17.so 0x00007f8a0f02e000 160K /usr/lib64/ld-2.17.so
示例2:heap
命令:jmap -heap pid
描述:显示Java堆详细信息
打印一个堆的摘要信息,包括使用的GC算法、堆配置信息和各内存区域内存使用信息
[root@searching-search-service-7cbfd67967-lczgh /]# jmap -heap 19 Attaching to process ID 19, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 using parallel threads in the new generation. using thread-local object allocation. Concurrent Mark-Sweep GC Heap Configuration: # 堆配置情况 MinHeapFreeRatio = 40 # 最小堆的使用比例 MaxHeapFreeRatio = 70 # 最大堆的可用比例 MaxHeapSize = 1342177280 (1280.0MB) # 最大堆空间大小 NewSize = 447348736 (426.625MB) # 新生代分配大小 MaxNewSize = 447348736 (426.625MB) # 最大可用新生代分配大小 OldSize = 894828544 (853.375MB) # 老年代大小 NewRatio = 2 # 新生代比例 SurvivorRatio = 8 # 新生代与suvivor的比例 MetaspaceSize = 268435456 (256.0MB) # 元空间 CompressedClassSpaceSize = 1073741824 (1024.0MB) # 压缩类空间 MaxMetaspaceSize = 268435456 (256.0MB) # 最大元空间 G1HeapRegionSize = 0 (0.0MB) # G1堆区大小 Heap Usage: # 堆使用情况 New Generation (Eden + 1 Survivor Space): # 新生代(伊甸区 + survior空间) capacity = 402653184 (384.0MB) # 容量 used = 304785928 (290.66651153564453MB) # 已经使用大小 free = 97867256 (93.33348846435547MB) # 剩余容量 75.69440404574077% used # 使用比例 Eden Space: # 伊甸区 capacity = 357957632 (341.375MB) used = 304352432 (290.2530975341797MB) free = 53605200 (51.12190246582031MB) 85.02470817551949% used From Space: # survior1区 capacity = 44695552 (42.625MB) used = 433496 (0.41341400146484375MB) free = 44262056 (42.211585998535156MB) 0.9698862204453812% used To Space: # survior2 区 capacity = 44695552 (42.625MB) used = 0 (0.0MB) free = 44695552 (42.625MB) 0.0% used concurrent mark-sweep generation: # 老生代使用情况 capacity = 894828544 (853.375MB) used = 227389600 (216.85562133789062MB) free = 667438944 (636.5193786621094MB) 25.41152732830123% used
示例3:histo[:live]
命令:jmap -histo:live pid
描述:显示堆中对象的统计信息。
其中包括每个Java类、对象数量、内存大小(单位:字节)、完全限定的类名。打印的虚拟机内部的类名称将会带有一个’*’前缀。如果指定了live子选项,则只计算活动的对象。
# 输出到文件 jmap -histo:live 19 > tmp.txt # 查看文件 [root@searching-search-service-7cbfd67967-lczgh /]# more tmp.txt num #instances #bytes class name ---------------------------------------------- 1: 13377 117973992 [B 2: 153786 25652312 [C 3: 27427 11856152 [Ljava.lang.Object; 4: 150553 3613272 java.lang.String 5: 3432 2251392 io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueue 6: 17724 1967136 java.lang.Class 7: 527 1848784 [Ljava.nio.channels.SelectionKey; 8: 52999 1695968 java.util.concurrent.ConcurrentHashMap$Node 9: 15386 1353968 java.lang.reflect.Method 10: 39224 1255168 java.util.HashMap$Node 11: 9383 970856 [I 12: 196 785504 [Ljava.nio.ByteBuffer; 13: 14480 695040 java.util.HashMap 14: 6187 669000 [Ljava.util.HashMap$Node; 15: 935 470232 [Ljava.util.concurrent.ConcurrentHashMap$Node; 16: 1233 463608 java.lang.Thread 17: 27454 439264 java.lang.Object 18: 553 338000 [Lio.netty.util.Recycler$DefaultHandle; 19: 576 327168 com.alibaba.arthas.deps.io.netty.util.internal.shaded.org.jctools.queues.MpscUnboundedArrayQueue 20: 7509 313696 [Ljava.lang.String; 21: 5554 311024 java.util.LinkedHashMap 22: 12181 292344 java.util.ArrayList 23: 8294 265408 java.lang.ref.WeakReference 24: 10749 257976 org.apache.catalina.loader.ResourceEntry 25: 6261 250440 java.lang.ref.SoftReference 26: 10040 240960 javassist.bytecode.Utf8Info 27: 5984 239360 java.util.LinkedHashMap$Entry 28: 9934 213088 [Ljava.lang.Class; 29: 2621 209680 java.lang.reflect.Constructor 30: 6322 202304 java.lang.ThreadLocal$ThreadLocalMap$Entry 31: 2685 193320 java.lang.reflect.Field 32: 5932 189824 java.util.Hashtable$Entry
示例四:clstats
命令:jmap -clstats pid
描述:打印类加载器信息(过程略慢)
-clstats是-permstat的替代方案,在JDK8之前,-permstat用来打印类加载器的数据
打印Java堆内存的永久保存区域的类加载器的智能统计信息。对于每个类加载器而言,它的名称、活跃度、地址、父类加载器、它所加载的类的数量和大小都会被打印。此外,包含的字符串数量和大小也会被打印。
jmap -clstats 19 Attaching to process ID 19, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 finding class loader instances ..
示例五:finalizerinfo
命令:jmap -finalizerinfo pid
描述:打印等待终结的对象信息
[root@searching-search-service-7cbfd67967-lczgh /]# jmap -finalizerinfo 19 Attaching to process ID 19, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 Number of objects pending for finalization: 0
Number of objects pending for finalization: 0 说明当前F-QUEUE队列中并没有等待Fializer线程执行final
示例六:dump:<dump-options>
命令:jmap -dump:format=b,file=heapdump.hprof pid
描述:生成堆转储快照dump文件。
以hprof二进制格式转储Java堆到指定filename的文件中。
live子选项是可选的。如果指定了live子选项,堆中只有活动的对象会被转储。想要浏览heap dump,可以将进程的内存heap输出出来到文件里,
再配合JProfiler(Java性能分析)、MAT(内存分析工具(Memory Analysis Tool)或与jhat (Java Heap Analysis Tool)一起使用,能够以图像的形式直观的展示当前内存是否有问题。
这个命令执行,JVM会将整个heap的信息dump写入到一个文件,heap如果比较大的话,就会导致这个过程比较耗时,并且执行的过程中为了保证dump的信息是可靠的,可能会暂停应用, 线上系统慎用。
[root@searching-search-api-fcc75c5f5-2frzt /]# jmap -dump:format=b,file=heapdump.hprof 19 Dumping heap to /heapdump.hprof ... Heap dump file created
未经允许请勿转载:程序喵 » jvm 指令工具 jmap 命令(Java内存映射)