1 jmap
1.1 命令格式
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) 连接到远程调试服务options 说明:
> pid: 目标进程的PID,进程编号,可以采用ps -ef | grep java 查看java进程的PID;
> executable: 产生core dump的java可执行程序;
> core: 将被打印信息的core dump文件;
> remote-hostname-or-IP: 远程debug服务的主机名或ip;
> server-id: 唯一id,假如一台主机上多个远程debug服务;
1.2 常用命令
1.2.1 查看类列表,包含实例数、占用内存大小
jmap -histo:live [pid]
VM的内部类名字开头会加上前缀”*”. 如果live子参数加上后,只统计活的对象数量
jmap -histo 2972 >c:\s.txt
jmap -histo 2972
num #instances #bytes class name (module)
-------------------------------------------------------1: 22918655 2200190880 com.ccf.bhf.track.model.track.AisTrackData2: 53697011 2132331976 [B (java.base@17.0.8)3: 50841113 1220186712 java.lang.String (java.base@17.0.8)4: 40550008 973200192 java.util.concurrent.ConcurrentSkipListMap$Node (java.base@17.0.8)5: 191171 622123520 [I (java.base@17.0.8)6: 4417702 388757776 com.ccf.bhf.track.model.track.RadarTrackData7: 9405048 300961536 java.util.concurrent.ConcurrentHashMap$Node (java.base@17.0.8)8: 9076630 217839120 java.util.concurrent.ConcurrentSkipListMap$Index (java.base@17.0.8)9: 2625424 102562312 [Ljava.lang.Object; (java.base@17.0.8)
采用jmap -histo pid>a.log日志将其保存,在一段时间后,使用文本对比工具,可以对比出GC回收了哪些对象。
jmap -dump:format=b,file=outfile 3024可以将3024进程的内存heap输出出来到outfile文件里,再配合MAT(内存分析工具)。
jvmdump_42">1.2.2 生成jvm的堆转储快照dump文件
jmap -dump:format=b,file=dumpFileName [pid]
live子选项是可选的,假如指定live选项,那么只输出活的对象到文件
jmap -dump:format=b,file=c:\heap.hprof 2972
jmap -dump:live,format=b,file=myjmapfile.hprof 2972
1.2.3 查看堆使用情况
--jdk 1.8
jmap -heap [pid]
--jdk 1.8 以后
jhsdb jmap --heap --pid [pid]
Attaching to process ID 30480, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 17.0.9+11-LTS-201using thread-local object allocation.
Garbage-First (G1) GC with 15 thread(s)Heap Configuration:MinHeapFreeRatio = 40MaxHeapFreeRatio = 70MaxHeapSize = 4221566976 (4026.0MB)NewSize = 1363144 (1.2999954223632812MB)MaxNewSize = 2531262464 (2414.0MB)OldSize = 5452592 (5.1999969482421875MB)NewRatio = 2SurvivorRatio = 8MetaspaceSize = 22020096 (21.0MB)CompressedClassSpaceSize = 1073741824 (1024.0MB)MaxMetaspaceSize = 17592186044415 MBG1HeapRegionSize = 2097152 (2.0MB)Heap Usage:
G1 Heap:regions = 2013capacity = 4221566976 (4026.0MB)used = 122679888 (116.99665832519531MB)free = 4098887088 (3909.0033416748047MB)2.906027280804653% used
G1 Young Generation:
Eden Space:regions = 28capacity = 102760448 (98.0MB)used = 58720256 (56.0MB)free = 44040192 (42.0MB)57.142857142857146% used
Survivor Space:regions = 0capacity = 0 (0.0MB)used = 0 (0.0MB)free = 0 (0.0MB)0.0% used
G1 Old Generation:regions = 33capacity = 83886080 (80.0MB)used = 63959632 (60.99665832519531MB)free = 19926448 (19.003341674804688MB)76.24582290649414% used
解析:
using parallel threads in the new generation. ##新生代采用的是并行线程处理方式using thread-local object allocation. Concurrent Mark-Sweep GC ##同步并行垃圾回收Heap Configuration: ##堆配置情况,也就是JVM参数配置的结果[平常说的tomcat配置JVM参数,就是在配置这些]MinHeapFreeRatio = 40 ##最小堆使用比例MaxHeapFreeRatio = 70 ##最大堆可用比例MaxHeapSize = 2147483648 (2048.0MB) ##最大堆空间大小NewSize = 268435456 (256.0MB) ##新生代分配大小MaxNewSize = 268435456 (256.0MB) ##最大可新生代分配大小OldSize = 5439488 (5.1875MB) ##老年代大小NewRatio = 2 ##新生代比例SurvivorRatio = 8 ##新生代与suvivor的比例PermSize = 134217728 (128.0MB) ##perm区 永久代大小MaxPermSize = 134217728 (128.0MB) ##最大可分配perm区 也就是永久代大小Heap Usage: ##堆使用情况【堆内存实际的使用情况】New Generation (Eden + 1 Survivor Space): ##新生代(伊甸区Eden区 + 幸存区survior(1+2)空间)capacity = 241631232 (230.4375MB) ##伊甸区容量used = 77776272 (74.17323303222656MB) ##已经使用大小free = 163854960 (156.26426696777344MB) ##剩余容量32.188004570534986% used ##使用比例Eden Space: ##伊甸区capacity = 214827008 (204.875MB) ##伊甸区容量used = 74442288 (70.99369812011719MB) ##伊甸区使用free = 140384720 (133.8813018798828MB) ##伊甸区当前剩余容量34.65220164496263% used ##伊甸区使用情况From Space: ##survior1区capacity = 26804224 (25.5625MB) ##survior1区容量used = 3333984 (3.179534912109375MB) ##surviror1区已使用情况free = 23470240 (22.382965087890625MB) ##surviror1区剩余容量12.43827838477995% used ##survior1区使用比例To Space: ##survior2 区capacity = 26804224 (25.5625MB) ##survior2区容量used = 0 (0.0MB) ##survior2区已使用情况free = 26804224 (25.5625MB) ##survior2区剩余容量0.0% used ## survior2区使用比例PS Old Generation: ##老年代使用情况capacity = 1879048192 (1792.0MB) ##老年代容量used = 30847928 (29.41887664794922MB) ##老年代已使用容量free = 1848200264 (1762.5811233520508MB) ##老年代剩余容量1.6416783843721663% used ##老年代使用比例Perm Generation: ##永久代使用情况capacity = 134217728 (128.0MB) ##perm区容量used = 47303016 (45.111671447753906MB) ##perm区已使用容量free = 86914712 (82.8883285522461MB) ##perm区剩余容量35.24349331855774% used ##perm区使用比例