天天看點

【Java 虛拟機原理】垃圾回收算法 ( 設定 JVM 指令參數輸出 GC 日志 | GC 日志輸出示例 | GC 日志分析 )

文章目錄

  • ​​一、設定 JVM 指令參數輸出 GC 日志​​
  • ​​二、GC 日志示例​​
  • ​​三、GC 日志分析​​

一、設定 JVM 指令參數輸出 GC 日志

在 IntelliJ IDEA 的啟動參數中設定

-XX:+PrintGCDetails      

Java 虛拟機參數 , 當運作 Java 程式時 , 會在控制台列印 GC 回收相關資訊 ;

​其它的 Java 虛拟機常用指令參數參考 ​

選擇 IntelliJ IDEA 中 , 運作程式 下拉菜單 中的 " Edit Configurations… " 選項 ;

【Java 虛拟機原理】垃圾回收算法 ( 設定 JVM 指令參數輸出 GC 日志 | GC 日志輸出示例 | GC 日志分析 )

在 VM options 輸入框中 , 輸入 ​

​-XX:+PrintGCDetails​

​ 選項 , 這是給 Java 虛拟機設定的參數 ;

【Java 虛拟機原理】垃圾回收算法 ( 設定 JVM 指令參數輸出 GC 日志 | GC 日志輸出示例 | GC 日志分析 )

二、GC 日志示例

​運作如下代碼 :​

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main = null;
        System.gc();
    }
}      

​指令行輸出的 GC 日志 :​

[GC (System.gc()) [PSYoungGen: 7895K->744K(153088K)] 7895K->752K(502784K), 0.0125267 secs] [Times: user=0.00 sys=0.00, real=0.03 secs] 
[Full GC (System.gc()) [PSYoungGen: 744K->0K(153088K)] [ParOldGen: 8K->593K(349696K)] 752K->593K(502784K), [Metaspace: 3012K->3012K(1056768K)], 0.0039947 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 153088K, used 3947K [0x0000000715b80000, 0x0000000720600000, 0x00000007c0000000)
  eden space 131584K, 3% used [0x0000000715b80000,0x0000000715f5af98,0x000000071dc00000)
  from space 21504K, 0% used [0x000000071dc00000,0x000000071dc00000,0x000000071f100000)
  to   space 21504K, 0% used [0x000000071f100000,0x000000071f100000,0x0000000720600000)
 ParOldGen       total 349696K, used 593K [0x00000005c1200000, 0x00000005d6780000, 0x0000000715b80000)
  object space 349696K, 0% used [0x00000005c1200000,0x00000005c1294520,0x00000005d6780000)
 Metaspace       used 3042K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 330K, capacity 388K, committed 512K, reserved 1048576K      
【Java 虛拟機原理】垃圾回收算法 ( 設定 JVM 指令參數輸出 GC 日志 | GC 日志輸出示例 | GC 日志分析 )

三、GC 日志分析

[GC (System.gc()) [PSYoungGen: 7895K->744K(153088K)] 7895K->752K(502784K), 0.0125267 secs] [Times: user=0.00 sys=0.00, real=0.03 secs]      

​GC (System.gc()) :​

​GC (System.gc())​

​​ 表示是開發者手動調用了 ​

​System.gc()​

​ 方法 ;

​[PSYoungGen: 7895K->744K(153088K)] :​

​PSYoungGen​

​ , 其中 PS 是 Parallel Seavenge 垃圾回收器 , YoungGen 是年輕代 ;

7895K->744K 表示垃圾回收 , 從占用 7895K 記憶體 , 變為占用 744K 記憶體 ;

153088K 表示年輕代 記憶體大小 ;

​[Times: user=0.00 sys=0.00, real=0.03 secs] :​

Times 表示本次垃圾回收基本耗時 ;

[Full GC (System.gc()) [PSYoungGen: 744K->0K(153088K)] [ParOldGen: 8K->593K(349696K)] 752K->593K(502784K), [Metaspace: 3012K->3012K(1056768K)], 0.0039947 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]      

​[ParOldGen: 8K->593K(349696K)] :​

Par 表示 Parallel 垃圾回收器 , OldGen 表示老年代 ;

​[Times: user=0.00 sys=0.00, real=0.00 secs] :​

Times 表示本次垃圾回收基本耗時 ;

PSYoungGen      total 153088K, used 3947K [0x0000000715b80000, 0x0000000720600000, 0x00000007c0000000)
  eden space 131584K, 3% used [0x0000000715b80000,0x0000000715f5af98,0x000000071dc00000)
  from space 21504K, 0% used [0x000000071dc00000,0x000000071dc00000,0x000000071f100000)
  to   space 21504K, 0% used [0x000000071f100000,0x000000071f100000,0x0000000720600000)      

1

1

1 行 ​

​PSYoungGen total 153088K, used 3947K [0x0000000715b80000, 0x0000000720600000, 0x00000007c0000000)​

​ 表示年輕代記憶體空間總大小 , 使用了多少 ;

2

2

2 行 ​

​eden space 131584K, 3% used [0x0000000715b80000,0x0000000715f5af98,0x000000071dc00000)​

​ 表示 Eden 區大小 , 以及使用情況 ;

3

3

3 行 ​

​from space 21504K, 0% used [0x000000071dc00000,0x000000071dc00000,0x000000071f100000)​

​ 表示 From 區大小 , 以及使用情況 ;

4

4

4 行 ​

​to space 21504K, 0% used [0x000000071f100000,0x000000071f100000,0x0000000720600000)​

​ 表示 To 區大小 , 以及使用情況 ;

ParOldGen       total 349696K, used 593K [0x00000005c1200000, 0x00000005d6780000, 0x0000000715b80000)
  object space 349696K, 0% used [0x00000005c1200000,0x00000005c1294520,0x00000005d6780000)