System.gc()日志分析

本文通过一个示例程序详细解析了Java中的垃圾回收机制,包括Young GC和Full GC的过程,展示了不同条件下堆内存的使用情况,深入理解Java内存管理和优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开日志:运行配置---XX:+PrintGCDetails

示例程序:

package com.test;

public class Test
{
    private Object instance = null;
    private String str;
    
    private static final int _1MB = 1024 * 1024;
    /** 这个成员属性唯一的作用就是占用一点内存 */
    private byte[] bigSize = new byte[2 * _1MB];
    
    public static void main(String[] args)
    {
        Test objectA = new Test();
        objectA.str = objectA.bigSize.toString();
        Test objectB = new Test();
        objectB.str = objectB.bigSize.toString();
        objectA.instance = objectA;
        objectB.instance = objectB;
        
        objectA = null;
        objectB = null;
        
        System.gc();
    }
}

打印:

[GC [PSYoungGen: 5980K->584K(229376K)] 5980K->584K(491520K), 0.0020351 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC [PSYoungGen: 584K->0K(229376K)] [ParOldGen: 0K->465K(262144K)] 584K->465K(491520K) [PSPermGen: 2573K->2572K(131072K)], 0.0116565 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 229376K, used 7864K [0x00000000f0000000, 0x0000000100000000, 0x0000000100000000)
  eden space 196608K, 4% used [0x00000000f0000000,0x00000000f07ae210,0x00000000fc000000)
  from space 32768K, 0% used [0x00000000fc000000,0x00000000fc000000,0x00000000fe000000)
  to   space 32768K, 0% used [0x00000000fe000000,0x00000000fe000000,0x0000000100000000)
 ParOldGen       total 262144K, used 465K [0x00000000e0000000, 0x00000000f0000000, 0x00000000f0000000)
  object space 262144K, 0% used [0x00000000e0000000,0x00000000e00744a0,0x00000000f0000000)
 PSPermGen       total 131072K, used 2579K [0x00000000d0000000, 0x00000000d8000000, 0x00000000e0000000)
  object space 131072K, 1% used [0x00000000d0000000,0x00000000d0284e98,0x00000000d8000000)

 

注释掉objectA = null; 和 objectB = null;

打印:

[GC [PSYoungGen: 9912K->4680K(229376K)] 9912K->4680K(491520K), 0.0092221 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC [PSYoungGen: 4680K->0K(229376K)] [ParOldGen: 0K->4561K(262144K)] 4680K->4561K(491520K) [PSPermGen: 2573K->2572K(131072K)], 0.0155152 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
Heap
 PSYoungGen      total 229376K, used 5898K [0x00000000f0000000, 0x0000000100000000, 0x0000000100000000)
  eden space 196608K, 3% used [0x00000000f0000000,0x00000000f05c29c0,0x00000000fc000000)
  from space 32768K, 0% used [0x00000000fc000000,0x00000000fc000000,0x00000000fe000000)
  to   space 32768K, 0% used [0x00000000fe000000,0x00000000fe000000,0x0000000100000000)
 ParOldGen       total 262144K, used 4561K [0x00000000e0000000, 0x00000000f0000000, 0x00000000f0000000)
  object space 262144K, 1% used [0x00000000e0000000,0x00000000e0474570,0x00000000f0000000)
 PSPermGen       total 131072K, used 2579K [0x00000000d0000000, 0x00000000d8000000, 0x00000000e0000000)
  object space 131072K, 1% used [0x00000000d0000000,0x00000000d0284e90,0x00000000d8000000)

 

日志信息解释:

YoungGC日志解释如下

FullGC

 

 

转载于:https://www.cnblogs.com/x-jingxin/p/9767473.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值