概述 jmap命令是一个可以输出所有内存中对象的工具,甚至可以将JVM中的heap,以二进制输出成文本。打印出某个java进程(使用pid)内存内的所有对象的情况(如:产生哪些对象,及其数量)。
命令格式 1 2 3 4 5 6 7 8 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) 连接到远程调试服务
基本命令 输出jvm的heap内容到文件 jmap -dump:live,format=b,file=[outputFileName.hprof] [pid]
使用hprof二进制形式 输出jvm的heap内容到文件。live子选项是可选的,只输出活的对象到文件。
打印正等候回收的对象的信息 jmap -finalizerinfo [pid]
打印heap的概要信息 JDK 8: jmap -heap [pid]
JDK 9往后:jhsdb jmap --heap --pid [pid]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 root@PC:~ Attaching to process ID 51266, please wait ... Debugger attached successfully. Server compiler detected. JVM version is 17.0.8.1+1-Ubuntu-0ubuntu122.04 using thread-local object allocation. Garbage-First (G1) GC with 10 thread(s) Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 4169138176 (3976.0MB) NewSize = 1363144 (1.2999954223632812MB) MaxNewSize = 2499805184 (2384.0MB) OldSize = 5452592 (5.1999969482421875MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 22020096 (21.0MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 2097152 (2.0MB) Heap Usage: G1 Heap: regions = 1988 capacity = 4169138176 (3976.0MB) used = 38374400 (36.5966796875MB) free = 4130763776 (3939.4033203125MB) 0.9204396299673038% used G1 Young Generation: Eden Space: regions = 3 capacity = 117440512 (112.0MB) used = 6291456 (6.0MB) free = 111149056 (106.0MB) 5.357142857142857% used Survivor Space: regions = 1 capacity = 2097152 (2.0MB) used = 2097152 (2.0MB) free = 0 (0.0MB) 100.0% used G1 Old Generation: regions = 16 capacity = 71303168 (68.0MB) used = 29985792 (28.5966796875MB) free = 41317376 (39.4033203125MB) 42.05394071691177% used
打印每个class的实例数目、内存占用、类名信息 jmap -histo:live [pid]