GC專題-查看日誌

GC日誌參數列表

-XX:+PrintGC 輸出GC日誌
-XX:+PrintGCDetails 輸出GC的詳細日誌
-XX:+PrintGCTimeStamps 輸出GC的時間戳(以基準時間的形式)
-XX:+PrintGCDateStamps 輸出GC的時間戳(以日期的形式,如 2013-05-04T21:53:59.234+0800)
-XX:+PrintHeapAtGC 在進行GC的前後打印出堆的信息
-Xloggc:../logs/gc.log 日誌文件的輸出路徑

備註:
GC的日誌是以替換的方式(>)寫入的,而不是追加(>>),如果下次寫入到同一個文件中的話,以前的GC內容會被清空。可以在程序啓動前或者停止時增加如下腳本,將之前gc.log轉儲到其它文件中。

if [ -f ${LOGS}/gc.log ]; then
    mv ${LOGS}/gc.log ${LOGS}/gc.log.`date +%Y%m%d%H%M%S`
fi

GC日誌示例

除了GC日誌參數外,爲了更加與生產環境一致,增加了如下幾個固定參數


-Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m -XX:MaxDirectMemorySize=1g -XX:SurvivorRatio=10 -XX:+UseConcMarkSweepGC

-XX:+PrintGC -Xloggc:gc.log

0.687: [GC (Allocation Failure)  873632K->751K(2009792K), 0.0024686 secs]
0.797: [GC (Allocation Failure)  874392K->805K(2009792K), 0.0016258 secs]
0.878: [GC (Allocation Failure)  874217K->765K(2009792K), 0.0014693 secs]
0.958: [GC (Allocation Failure)  874337K->703K(2009792K), 0.0015396 secs]
1.036: [GC (Allocation Failure)  874399K->852K(2009792K), 0.0022075 secs]
1.117: [GC (Allocation Failure)  874628K->772K(2009792K), 0.0015507 secs]
1.194: [GC (Allocation Failure)  874600K->788K(2009792K), 0.0014966 secs]

-XX:+PrintGC -XX:+PrintHeapAtGC -Xloggc:gc.log

{Heap before GC invocations=0 (full 0):
 par new generation   total 961216K, used 873632K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d28150, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.585: [GC (Allocation Failure)  873632K->765K(2009792K), 0.0022171 secs]
Heap after GC invocations=1 (full 0):
 par new generation   total 961216K, used 765K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=1 (full 0):
 par new generation   total 961216K, used 874406K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d2a518, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.701: [GC (Allocation Failure)  874406K->834K(2009792K), 0.0019298 secs]
Heap after GC invocations=2 (full 0):
 par new generation   total 961216K, used 834K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795e308a0, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}

-XX:+PrintGCDetails -XX:+PrintHeapAtGC -Xloggc:gc.log

{Heap before GC invocations=0 (full 0):
 par new generation   total 961216K, used 873632K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d28150, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.793: [GC (Allocation Failure) 0.793: [ParNew: 873632K->765K(961216K), 0.0039284 secs] 873632K->765K(2009792K), 0.0041837 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Heap after GC invocations=1 (full 0):
 par new generation   total 961216K, used 765K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=1 (full 0):
 par new generation   total 961216K, used 874406K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d2a518, 0x0000000795d60000)
  from space 87360K,   0% used [0x000000079b2b0000, 0x000000079b36f520, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
0.917: [GC (Allocation Failure) 0.917: [ParNew: 874406K->776K(961216K), 0.0015625 secs] 874406K->776K(2009792K), 0.0016277 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
Heap after GC invocations=2 (full 0):
 par new generation   total 961216K, used 776K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795e221c8, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3262K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}

-XX:+PrintGCDetails -XX:+PrintHeapAtGC -Xloggc:gc.log -XX:+PrintGCDateStamps

{Heap before GC invocations=0 (full 0):
 par new generation   total 961216K, used 873632K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d28150, 0x0000000795d60000)
  from space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3260K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
2020-02-26T14:50:12.302-0800: 0.579: [GC (Allocation Failure) 2020-02-26T14:50:12.302-0800: 0.579: [ParNew: 873632K->84744K(961216K), 0.0430026 secs] 873632K->84744K(2009792K), 0.0431113 secs] [Times: user=0.22 sys=0.05, real=0.04 secs] 
Heap after GC invocations=1 (full 0):
 par new generation   total 961216K, used 84744K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,  97% used [0x000000079b2b0000, 0x00000007a05723f0, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3260K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=1 (full 0):
 par new generation   total 961216K, used 958386K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,  99% used [0x0000000760800000, 0x0000000795d2a518, 0x0000000795d60000)
  from space 87360K,  97% used [0x000000079b2b0000, 0x00000007a05723f0, 0x00000007a0800000)
  to   space 87360K,   0% used [0x0000000795d60000, 0x0000000795d60000, 0x000000079b2b0000)
 concurrent mark-sweep generation total 1048576K, used 0K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
2020-02-26T14:50:12.445-0800: 0.723: [GC (Allocation Failure) 2020-02-26T14:50:12.445-0800: 0.723: [ParNew: 958386K->86052K(961216K), 0.0858331 secs] 958386K->170593K(2009792K), 0.0859161 secs] [Times: user=0.34 sys=0.08, real=0.09 secs] 
Heap after GC invocations=2 (full 0):
 par new generation   total 961216K, used 86052K [0x0000000760800000, 0x00000007a0800000, 0x00000007a0800000)
  eden space 873856K,   0% used [0x0000000760800000, 0x0000000760800000, 0x0000000795d60000)
  from space 87360K,  98% used [0x0000000795d60000, 0x000000079b169330, 0x000000079b2b0000)
  to   space 87360K,   0% used [0x000000079b2b0000, 0x000000079b2b0000, 0x00000007a0800000)
 concurrent mark-sweep generation total 1048576K, used 84540K [0x00000007a0800000, 0x00000007e0800000, 0x00000007e0800000)
 Metaspace       used 3261K, capacity 4556K, committed 4864K, reserved 1056768K
  class space    used 320K, capacity 392K, committed 512K, reserved 1048576K
}

GC日誌解讀

上面日誌基本上都有如下這一行,就各段進行解讀下

示例
2020-02-26T14:50:12.445-0800: 0.723: [GC (Allocation Failure) 2020-02-26T14:50:12.445-0800: 0.723: [ParNew: 958386K->86052K(961216K), 0.0858331 secs] 958386K->170593K(2009792K), 0.0859161 secs] [Times: user=0.34 sys=0.08, real=0.09 secs] 


// gc發生的時間點
2020-02-26T14:50:12.445-0800: 0.723: 

// GC (分配 失敗)
[GC (Allocation Failure) 2020-02-26T14:50:12.445-0800: 0.723: 

//年青代gc收集器是ParNew:年青代(eden+from+to)整體佔用了(961216K),其中部分年青代(eden+from)空間回收前佔用了958386K,回收後變成了86052K,耗時0.0859161 secs
[ParNew: 958386K->86052K(961216K), 0.0858331 secs] 

//整個堆回收前的佔用了958386K->回收後的佔用了170593K,整個堆大小(2009792K)
958386K->170593K(2009792K), 0.0859161 secs] 

//用戶耗時0.34秒 , 系統耗時0.08秒 , 實際耗時 0.09秒
[Times: user=0.34 sys=0.08, real=0.09 secs] 

GC日誌耗時解讀

你是否對上面這段理解的我一樣一知半解,下面就詳細說明下

//用戶耗時0.34秒 , 系統耗時0.08秒 , 實際耗時 0.09秒
[Times: user=0.34 sys=0.08, real=0.09 secs] 

官方英文說明

  • Real is wall clock time – time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).

  • User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

  • Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like ‘user’, this is only CPU time used by the process.

中文說明

  • real —— 程序從開始到結束所用的時鐘時間。這個時間包括其他進程使用的時間片和進程阻塞的時間(比如等待 I/O 完成)。

  • user —— 進程執行用戶態代碼(核心之外)所使用的時間。這是執行此進程所使用的實際 CPU 時間,其他進程和此進程阻塞的時間並不包括在內。在垃圾收集的情況下,表示 GC 線程執行所使用的 CPU 總時間。

  • sys —— 進程在內核態消耗的 CPU 時間,即在內核執行系統調用或等待系統事件所使用的 CPU 時間。

解讀
user + sys 時間告訴我們程序執行實際使用的 CPU 時間。注意這裏指所有的 CPU,因此如果在進程裏有多個線程的話,這個時間可能會超過 real 所表示的時鐘時間。
這也就是上面日誌中 user(0.34) + sys(0.08) > real(0.09 secs)的原因。
所以我們觀察單次gc時間開銷主要是看real字段的耗時情況。

附:本文使用的java代碼

public class GarbageCollectorDemo {
    public static void main(String[] args) {
        /* -XX:+UseParallelOldGC和-XX:+UseParallelGC結果一樣,因爲MXBean名字一樣,但是實際使用的不一樣 */
        List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean gcbean : gcbeans) {
            System.out.println("alioolog gcbean:" + gcbean.getName());
        }
        List list2 = new ArrayList();
        for (int i = 0; i < 10000; i++) {
            List list = new ArrayList();
            byte[] b = new byte[1_000_000];
            list.add(b);

            if (i % 10 == 0) {
                list2.add(b);
            }
        }

        try {
            Thread.sleep(10 * 60 * 1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章