拜託,別再問我GC垃圾回收日誌怎麼看了

日誌的重要性,不須要過多強調了。經過日誌,咱們能夠發現程序可能有內存(泄露)問題。本文從案例出發,具體介紹這些日誌信息,以期幫助你們更好地瞭解垃圾回收的運行狀況。
java

仍是先上圖,看看本文的主要內容:web

5f41636fcf47e86f72260f519ac37289.png

咱們先來看個案例,代碼以下:spring

/**
 * 打印垃圾回收日誌案例
 * 參數設置: -XX:+PrintGCDetails
 * @author 田維常
 * @version 1.0
 * @date 2020/11/9 8:22
 */

public class PrintGCDetailsDemo {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

啓動參數設置:app

XX:+PrintGCDetailside

運行main方法,輸出spa

Hello world
Heap
 PSYoungGen      total 38400K, used 4670K [0x00000000d5f00000, 0x00000000d8980000, 0x0000000100000000)
  eden space 33280K, 14% used [0x00000000d5f00000,0x00000000d638fb98,0x00000000d7f80000)
  from space 5120K, 0% used [0x00000000d8480000,0x00000000d8480000,0x00000000d8980000)
  to   space 5120K, 0% used [0x00000000d7f80000,0x00000000d7f80000,0x00000000d8480000)
 ParOldGen       total 87552K, used 0K [0x0000000081c000000x00000000871800000x00000000d5f00000)
  object space 87552K, 0% used [0x0000000081c00000,0x0000000081c00000,0x0000000087180000)
 Metaspace       used 3525K, capacity 4498K, committed 4864K, reserved 1056768K
  class space    used 388K, capacity 390K, committed 512K, reserved 1048576K

關於GC日誌的參數3d

-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 日誌文件的輸出路徑


參數案例


-XX:+PrintGCDetails -XX:-UseAdaptiveSizePolicy -XX:SurvivorRatio=8 -XX:NewSize=10M -XX:MaxNewSize=10M

參數解釋:日誌

-XX:+PrintGCDetails 啓用日誌code

-XX:-UseAdaptiveSizePolicy 禁用動態調整,使SurvivorRatio能夠起做用orm

-XX:SurvivorRatio=8設置Eden:Survivior=8:1

-XX:NewSize=10M -XX:MaxNewSize=10M設置整個新生代的大小爲10M

寫了一個Spring Boot項目,很是簡單的項目,裏面寫了一個controller

package com.tian.my_code.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 田維常
 * @version 1.0
 * @date 2020/11/8 15:46
 */

@RestController
public class GCController {

    List<Object> strings = new ArrayList<>();

    @GetMapping("/gc")
    public String addObject() {
        System.out.println("-------gc-------");
        for (int i = 0; i < 1000000; i++){
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int [] a=new int[500000];
            strings.add(a);
        }
        return "ok";
    }
}

這段代碼實際上是想後面演示OOM用的,都行吧,這裏先用他來看看GC垃圾回收日誌。

在IDEA中設置

491ddaeabe18cb71c788b26ede31934b.png

輸出結果

8b446f40ea43650a47595e9aca1f3351.png

抓出一段來聊聊


GC (minor )日誌


[GC (Allocation Failure) [PSYoungGen: 8525K->352K(9216K)] 98695K->98486K(130048K), 0.0092873 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 

(Allocation Failure):代表本次引發GC的緣由是由於在年輕代中沒有足夠的空間可以存儲新的數據了。

PSYoungGen:表示是GC類型

8525KYoungGC前新生代內存佔用

352KYoungGC新生代內存佔用

9216K:新生代總共大小

98695K:YoungGC前JVM內存佔用

98486K:YoungGC後JVM內存使用

130048K:JVM堆總大小

0.0092873 secsYoungGC耗時

user=0.00YoungGC用戶耗時

sys=0.00YoungGC系統耗時

real=0.01:YoungGC實際耗時(這裏竟然四捨五入了)


Full GC 日誌


[Full GC (Ergonomics) [PSYoungGen: 8051K->7817K(9216K)] [ParOldGen: 244969K->244969K(245760K)] 253020K->252786K(254976K), [Metaspace: 29386K->29386K(1077248K)], 0.0525381 secs] [Times: user=0.13 sys=0.00, real=0.05 secs] 

PSYoungGen: 8051K->7817K(9216K):新生代區GC先後和總大小

ParOldGen: 244969K->244969K(245760K):老年代GC先後和大小。

253020K->252786K(254976K):堆GC先後內存佔用狀況。

Metaspace: 29386K->29386K(1077248K):元空間GC先後和總大小

後面那幾項和上面同樣

GC垃圾回收日誌就是這麼簡單麼~藍(nan)不藍(nan)?

相關文章
相關標籤/搜索