JVM學習之1 GC日誌理解
為了觀察GC日誌,我們需要設定JVM啟動引數:
-XX:+PrintGCDetails-----------------------------表示詳細的GC日誌的輸出
下面程式程式碼是我摘抄《深入理解JVM》這個本書上的一個示例;
public class RefrenceCountingGC { public Object instance =null; private static final int _1MB=1024*1024; private byte[] bigSize=new byte[2*_1MB]; public static void main(String[] args) { RefrenceCountingGC refrenceCountingGC1=new RefrenceCountingGC(); RefrenceCountingGC refrenceCountingGC2=new RefrenceCountingGC(); refrenceCountingGC1.instance=refrenceCountingGC2; refrenceCountingGC2.instance=refrenceCountingGC1; refrenceCountingGC1=null; refrenceCountingGC2=null; System.gc(); } }
我是在Intellij下執行的,下面檢視GC日誌:
[GC (System.gc()) [PSYoungGen: 11112K->1865K(38400K)] 11112K->1873K(125952K), 0.0025381 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (System.gc()) [PSYoungGen: 1865K->0K(38400K)] [ParOldGen: 8K->1765K(87552K)] 1873K->1765K(125952K), [Metaspace: 4781K->4781K(1056768K)], 0.0084446 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] Heap PSYoungGen total 38400K, used 665K [0x00000000d5f80000, 0x00000000d8a00000, 0x0000000100000000) eden space 33280K, 2% used [0x00000000d5f80000,0x00000000d60267f0,0x00000000d8000000) from space 5120K, 0% used [0x00000000d8000000,0x00000000d8000000,0x00000000d8500000) to space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000) ParOldGen total 87552K, used 1765K [0x0000000081e00000, 0x0000000087380000, 0x00000000d5f80000) object space 87552K, 2% used [0x0000000081e00000,0x0000000081fb9668,0x0000000087380000) Metaspace used 4849K, capacity 5112K, committed 5248K, reserved 1056768K class space used 521K, capacity 536K, committed 640K, reserved 1048576K
分析第一句:
[GC (System.gc()) [PSYoungGen: 11112K->1865K(38400K)] 11112K->1873K(125952K), 0.0025381 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
GC (System.gc():表示GC的型別為GC,這裡的System.gc()的含義我是比較迷惑的,因為System.gc()會觸發Full GC。可是這裡並不是Full GC,而是GC。
[PSYoungGen: 11112K->1865K(38400K)] PSYoungGen:表示新生代區域;11112K->1865K(38400K)表示該記憶體區域記憶體總容量為37.5M,垃圾回收前佔用該塊區域記憶體為10.85M,垃圾回收後為1.82M。11112K->1873K(125952K),前面的資料幾乎不變,含義基本相同,後面的125952K表示java堆空間值為123M。0.0025381 secs表示GC佔用該記憶體區域的時間,單位是秒。
[Times: user=0.00 sys=0.00, real=0.00 secs] user代表程序在使用者態消耗的CPU時間,sys代表代表程序在核心態消耗的CPU時間、real代表程式從開始到結束所用的時鐘時間。這個時間包括其他程序使用的時間片和程序阻塞的時間(比如等待 I/O 完成)。