zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【Java 虚拟机原理】垃圾回收算法 ( 设置 JVM 命令参数输出 GC 日志 | GC 日志输出示例 | GC 日志分析 )

2023-09-27 14:28:11 时间





一、设置 JVM 命令参数输出 GC 日志



在 IntelliJ IDEA 的启动参数中设置

-XX:+PrintGCDetails

Java 虚拟机参数 , 当运行 Java 程序时 , 会在控制台打印 GC 回收相关信息 ;

其它的 Java 虚拟机常用命令参数参考 : https://blog.csdn.net/yangwei234/article/details/82977716


选择 IntelliJ IDEA 中 , 运行程序 下拉菜单 中的 " Edit Configurations… " 选项 ;

在这里插入图片描述

在 VM options 输入框中 , 输入 -XX:+PrintGCDetails 选项 , 这是给 Java 虚拟机设置的参数 ;

在这里插入图片描述





二、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

在这里插入图片描述





三、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 1PSYoungGen total 153088K, used 3947K [0x0000000715b80000, 0x0000000720600000, 0x00000007c0000000) 表示年轻代内存空间总大小 , 使用了多少 ;

2 2 2eden space 131584K, 3% used [0x0000000715b80000,0x0000000715f5af98,0x000000071dc00000) 表示 Eden 区大小 , 以及使用情况 ;

3 3 3from space 21504K, 0% used [0x000000071dc00000,0x000000071dc00000,0x000000071f100000) 表示 From 区大小 , 以及使用情况 ;

4 4 4to space 21504K, 0% used [0x000000071f100000,0x000000071f100000,0x0000000720600000) 表示 To 区大小 , 以及使用情况 ;


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

老年代区域的内存大小 , 及使用情况 ;