開啓-XX:+PrintGCDetails,具體日誌內容分析

看一段程序

package day20200314;

import java.lang.ref.SoftReference;

/**
 * @Author: xiaoshijiu
 * @Date: 2020/3/14
 * @Description: 分析GC打印的日誌具體內容
 */
public class GCDetailsAnalyze {

    public static void main(String[] args) {

        SoftReference<byte[]> softReference = new SoftReference<>(new byte[5 * 1024 * 1024]);

        System.gc();

        byte[] bytes = new byte[6 * 1024 * 1024];

    }

}

設置了JVM參數:

-Xmx10m -Xms10m -XX:+PrintGCDetails
最大堆內存10m、初始化堆內存10m、開啓了GC日誌輸出

首先都很清楚,軟引用的特點:
內存不足的時候,會被垃圾回收,內存足夠,不會被回收;

運行結果:

"C:\Program Files\Java\jdk1.8.0_111\bin\java.exe" -Xmx10m -Xms10m -XX:+PrintGCDetails...

[GC (System.gc()) [PSYoungGen: 2025K->488K(2560K)] 7145K->5824K(9728K), 0.0012223 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 488K->0K(2560K)] [ParOldGen: 5336K->5792K(7168K)] 5824K->5792K(9728K), [Metaspace: 3479K->3479K(1056768K)], 0.0073629 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 

[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 5792K->5792K(9728K), 0.0003410 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 5792K->5792K(9728K), 0.0004555 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 5792K->5792K(7168K)] 5792K->5792K(9728K), [Metaspace: 3479K->3479K(1056768K)], 0.0032795 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 5792K->5792K(9728K), 0.0004670 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 5792K->653K(7168K)] 5792K->653K(9728K), [Metaspace: 3479K->3479K(1056768K)], 0.0094774 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 

// 下面是堆內存總的概況,各區域所佔大小,以及被使用情況
Heap
 PSYoungGen      total 2560K, used 20K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 1% used [0x00000000ffd00000,0x00000000ffd05360,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 7168K, used 6797K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 94% used [0x00000000ff600000,0x00000000ffca3750,0x00000000ffd00000)
 Metaspace       used 3486K, capacity 4498K, committed 4864K, reserved 1056768K
  class space    used 387K, capacity 390K, committed 512K, reserved 1048576K

Process finished with exit code 0

總結的一張圖:

在這裏插入圖片描述
Full GC:對整個堆內存空間的一次垃圾回收
GC:對年輕代空間的一次垃圾回收
Allocation Failure:“分配失敗”,即爲新對象分派內存不夠
System.gc():執行該方法觸發的GC

程序分析:

  1. 首先軟引用指向的對象5m,主動進行GC,這裏的5m對象不會被回收;

    可以看出來GC( System.gc() )日誌, [ParOldGen: 5336K->5792K(7168K)]
    5m對象在老年代中,也並沒有被回收;

  2. 再創建一個6m對象,內存不夠,JVM進行GC清理垃圾,會回收上面的5m對象;

    可以看出來Full GC( Allocation Failure )日誌,[ParOldGen: 5792K->653K(7168K)]
    5m對象在老年代中被回收了;

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