JMap
首先要知道Java进程的pid。
Windows:
..
..
..
Linux:
ps -ef | grep java
查看堆栈信息(jmap -heap pid)
jmap -heap 8512 Attaching to process ID 8512, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 using thread-local object allocation. Parallel GC with 8 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 2128609280 (2030.0MB) NewSize = 44564480 (42.5MB) MaxNewSize = 709361664 (676.5MB) OldSize = 89653248 (85.5MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 68157440 (65.0MB) used = 57053032 (54.410011291503906MB) free = 11104408 (10.589988708496094MB) 83.70770967923679% used From Space: capacity = 5242880 (5.0MB) used = 3113008 (2.9687957763671875MB) free = 2129872 (2.0312042236328125MB) 59.37591552734375% used To Space: capacity = 3670016 (3.5MB) used = 0 (0.0MB) free = 3670016 (3.5MB) 0.0% used PS Old Generation capacity = 89653248 (85.5MB) used = 49152 (0.046875MB) free = 89604096 (85.453125MB) 0.05482456140350877% used 3998 interned Strings occupying 342784 bytes.
打印等待回收的对象信息(jmap -finalizerinfo pid)
jmap -finalizerinfo 8512 Attaching to process ID 8512, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 Number of objects pending for finalization: 0
打印堆里面对象的统计信息:对象数量、占用大小、类名(jmap -histo:live pid | more)
jmap -histo:live 8512 | more num #instances #bytes class name ---------------------------------------------- 1: 9728 1016408 [C 2: 1650 303784 [B 3: 9441 226584 java.lang.String 4: 1821 205672 java.lang.Class 5: 1285 92520 org.apache.zookeeper.data.StatPersisted 6: 1735 87008 [Ljava.lang.Object; 7: 2385 76320 java.util.HashMap$Node 8: 2322 74304 java.util.concurrent.ConcurrentHashMap$Node 9: 1199 57552 java.util.HashMap 10: 783 54312 [I 11: 658 45208 [Ljava.util.HashMap$Node; 12: 1285 41120 org.apache.zookeeper.server.DataNode 13: 437 38456 java.lang.reflect.Method 14: 502 36144 org.apache.zookeeper.server.Request 15: 882 35280 java.util.TreeMap$Entry 16: 501 24048 org.apache.zookeeper.txn.TxnHeader 17: 889 21336 java.util.LinkedList$Node 18: 582 21296 [Ljava.lang.String; 19: 1049 16784 java.util.HashSet 20: 501 16032 org.apache.zookeeper.server.quorum.QuorumPacket 21: 399 15960 java.lang.ref.SoftReference 22: 31 15952 [Ljava.util.concurrent.ConcurrentHashMap$Node;
打印类加载器统计信息(jmap -clstats pid)
jmap -clstats 8512 Attaching to process ID 8512, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.161-b12 finding class loader instances ..done. computing per loader stat ..done. please wait.. computing liveness....................................liveness analysis may be inaccurate ... class_loader classes bytes parent_loader alive? type <bootstrap> 1359 2420845 null live <internal> 0x000000008128cef8 1 1474 null dead sun/reflect/[email protected]0x0000000017289df8 0x000000008128d088 1 880 null dead sun/reflect/[email protected]0x0000000017289df8 0x000000008128d218 1 880 null dead sun/reflect/[email protected]0x0000000017289df8 0x00000000814621d0 0 0 0x000000008120b6c8 dead java/util/[email protected]0x0000000017303798 0x000000008120b6c8 358 716069 0x000000008120b728 live sun/misc/[email protected]0x000000001728f6a0 0x000000008120b728 3 2574 null live sun/misc/[email protected]0x000000001728fa48 0x000000008128cfc0 1 880 null dead sun/reflect/[email protected]0x0000000017289df8 0x000000008128d150 1 880 null dead sun/reflect/[email protected]0x0000000017289df8 total = 9 1725 3144482 N/A alive=3, dead=6 N/A
把堆信息生成一个文件(jmap -dump:live,format=b,file=heap.bin <pid>)
[d:\]$ jmap -dump:live,format=b,file=heap.bin 8512 Dumping heap to D:\heap.bin ... Heap dump file created
之后,我们利用Mat来分析,下载 Eclipse Memory Analyzer
..
下载完解压
...
..
..
..打开我们刚才生成的文件
..
..看效果
..
JStat
每隔一段时间输出GC情况(jstat -gcutil pid inteval)
[d:\]$ jstat -gcutil 8512 3000 S0 S1 E O M CCS YGC YGCT FGC FGCT GCT 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082 0.00 0.00 0.94 7.26 96.88 93.14 7 0.061 2 0.022 0.082
参数:
S0 from区已使用容量的百分比 S1 to区已使用容量的百分比 E 伊甸区已使用容量的百分比 O 老年代已使用容量的百分比 M 元数据空间利用率 CCS 压缩类空间利用率 YGC 从应用程序启动到采样时年轻代中gc次数 YGCT 从应用程序启动到采样时年轻代中gc所用时间(s) FGC 从应用程序启动到采样时old代(全gc)gc次数 FGCT 从应用程序启动到采样时old代(全gc)gc所用时间(s) GCT 从应用程序启动到采样时gc用的总时间(s)
还有另外一个方法,输出的要全面一些
[d:\]$ jstat -gc 8512 3000 S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 4096.0 3584.0 0.0 0.0 133120.0 1257.0 37376.0 2715.3 9600.0 9300.8 1152.0 1073.0 7 0.061 2 0.022 0.082 4096.0 3584.0 0.0 0.0 133120.0 1257.0 37376.0 2715.3 9600.0 9300.8 1152.0 1073.0 7 0.061 2 0.022 0.082 4096.0 3584.0 0.0 0.0 133120.0 1257.0 37376.0 2715.3 9600.0 9300.8 1152.0 1073.0 7 0.061 2 0.022 0.082 4096.0 3584.0 0.0 0.0 133120.0 1257.0 37376.0 2715.3 9600.0 9300.8 1152.0 1073.0 7 0.061 2 0.022 0.082
S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
S0U: Survivor space 0 utilization (kB).
S1U: Survivor space 1 utilization (kB).
EC: Current eden space capacity (kB).
EU: Eden space utilization (kB).
OC: Current old space capacity (kB).
OU: Old space utilization (kB).
MC: Metaspace capacity (kB).
MU: Metacspace utilization (kB).
CCSC: Compressed class space capacity (kB).
CCSU: Compressed class space used (kB).
YGC: Number of young generation garbage collection events.
YGCT: Young generation garbage collection time.
FGC: Number of full GC events.
FGCT: Full garbage collection time.
GCT: Total garbage collection time.
参考:https://docs.oracle.com/javase/8/docs/technotes/tools/windows/jstat.html
原文地址:https://www.cnblogs.com/LUA123/p/9995974.html