GcRoots

2021-01-06  本文已影响0人  陈萍儿Candy

1.虚拟机栈中局部变量(也叫局部变量表)中引用的对象
2.方法区中类的静态变量、常量引用的对象
3.本地方法栈中 JNI (Native方法)引用的对象

public class GCRootDemo {
    private byte[] byteArray = new byte[100 * 1024 * 1024];
 
    private static GCRootDemo gc2;
    private static final GCRootDemo gc3 = new GCRootDemo();
 
    public static void m1(){
        GCRootDemo gc1 = new GCRootDemo();
        System.gc();
        System.out.println("第一次GC完成");
    }
    public static void main(String[] args) {
        m1();
    }
}  

解释:

gc1:是虚拟机栈中的局部变量

gc2:是方法区中类的静态变量

gc3:是方法区中的常量

都可以作为GC Roots 的对象。

上一篇下一篇

猜你喜欢

热点阅读