揭开JVM垃圾回收的神秘面纱?JVM到底用的什么算法去发现垃圾的?附代码及GC日志 堆内存大小设置,垃圾对象会被回收几次?

/**
 * @Author: Be.insighted
 * Description:
 * @date Create on 2020/9/18 15:08
 **/
public class TestGC {

    public Object instance;
    private static final int _1MB = 1024*1024;
    private byte[] bigsize = new byte[2*_1MB];
    public static void testGC(){
        TestGC a = new TestGC();
        TestGC b = new TestGC();
        a.instance = b;
        b.instance = a;
        System.gc();
    }

    public static void main(String[] args) {
        testGC();
    }
}

-Xms10m  -Xmx20m -XX:+PrintGCDetails 早点触发GC

对象a、b循环依赖!!!

Java 是采用什么方法定位垃圾的?

 

[GC (Allocation Failure) [PSYoungGen: 2048K->490K(2560K)] 2048K->1018K(9728K), 0.0609787 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[GC (Allocation Failure) [PSYoungGen: 2538K->506K(4608K)] 3066K->1346K(11776K), 0.0020613 secs] [Times: user=0.03 sys=0.00, real=0.00 secs] 
[GC (System.gc()) [PSYoungGen: 4169K->506K(4608K)] 9105K->6340K(11776K), 0.0016052 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 506K->0K(4608K)] [ParOldGen: 5834K->6260K(7168K)] 6340K->6260K(11776K), [Metaspace: 3426K->3426K(1056768K)], 0.0094744 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 4608K, used 41K [0x00000000ff980000, 0x00000000ffe80000, 0x0000000100000000)
  eden space 4096K, 1% used [0x00000000ff980000,0x00000000ff98a548,0x00000000ffd80000)
  from space 512K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x00000000ffe00000)
  to   space 512K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000ffe80000)
 ParOldGen       total 7168K, used 6260K [0x00000000fec00000, 0x00000000ff300000, 0x00000000ff980000)
  object space 7168K, 87% used [0x00000000fec00000,0x00000000ff21d088,0x00000000ff300000)
 Metaspace       used 3438K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 363K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

有垃圾回收! <—— 说明JVM采用的是根可达算法进行垃圾回收的

根可达算法,如何定位根对象?

从根上对象开始搜索,根对象有以下几种

线程栈变量: 一个main方法开始运行,main中的线程栈调用了其他方法,main栈中的方法访问的对象叫跟对象

静态变量: T.class 对静态变量的初始化 能够访问到的对象叫根对象

常量池: 一个class对象能访问到其他class的对象

JNI指针:调用本地方法的对象

更多JAVA虚拟机理论知识

/**
 * @Author: Be.insighted
 * Description:
 * @date Create on 2020/9/18 15:08
 **/
public class TestGC {
    public static TestGC testGC = null;
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("执行finalize() 方法");
    }
    public static void main(String[] args) throws Throwable {
        testGC = new TestGC();
        testGC = null;
        System.gc();
        Thread.sleep(500);
        if (testGC != null) {
            System.out.println("还没被回收");
        } else {
            System.out.println("已回收");
        }
        testGC = null;
        System.gc();
        Thread.sleep(500);

        if (testGC != null) {
            System.out.println("还没被回收");
        } else {
            System.out.println("已回收");
        }
    }
}

[GC (Allocation Failure) [PSYoungGen: 2554K->490K(2560K)] 3394K->1949K(9728K), 0.0088334 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (System.gc()) [PSYoungGen: 1954K->506K(4608K)] 3412K->2241K(11776K), 0.0084512 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 
[Full GC (System.gc()) [PSYoungGen: 506K->0K(4608K)] [ParOldGen: 1734K->1852K(7168K)] 2241K->1852K(11776K), [Metaspace: 3321K->3321K(1056768K)], 0.0091280 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 
执行finalize() 方法
已回收
[GC (System.gc()) [PSYoungGen: 224K->32K(4608K)] 2076K->1884K(11776K), 0.0837842 secs] [Times: user=0.00 sys=0.00, real=0.08 secs] 
[Full GC (System.gc()) [PSYoungGen: 32K->0K(4608K)] [ParOldGen: 1852K->1596K(7168K)] 1884K->1596K(11776K), [Metaspace: 3435K->3435K(1056768K)], 0.0624987 secs] [Times: user=0.03 sys=0.00, real=0.06 secs] 
已回收
Heap
 PSYoungGen      total 4608K, used 41K [0x00000000ff980000, 0x00000000ffe80000, 0x0000000100000000)
  eden space 4096K, 1% used [0x00000000ff980000,0x00000000ff98a5a8,0x00000000ffd80000)
  from space 512K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x00000000ffe00000)
  to   space 512K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000ffe80000)
 ParOldGen       total 7168K, used 1596K [0x00000000fec00000, 0x00000000ff300000, 0x00000000ff980000)
  object space 7168K, 22% used [0x00000000fec00000,0x00000000fed8f260,0x00000000ff300000)
 Metaspace       used 3442K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 363K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

去掉

并不会立即执行垃圾回收!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值