简体   繁体   English

对象是否符合“obj = null”后的垃圾回收?

[英]Is object eligible for garbage collection after “obj = null”?

I know System.gc() is not guaranteed to cause GC, but theoretically, in the following code, will the object obj be eligible for garbage collection? 我知道System.gc()不能保证会导致GC,但理论上,在下面的代码中,对象obj有资格进行垃圾回收?

public class Demo {

    public static void main(String[] args) throws Exception {
        SomeClass obj = new SomeClass();
        ArrayList list = new ArrayList();
        list.add(obj);
        obj = null;
        System.gc();
    }

}

class SomeClass {
    protected void finalize() {
        System.out.println("Called");
    }
}

At the point where you call System.gc() the SomeClass instance you created is not eligible for garbage collection because it is still referred to by the list object, ie it is still reachable . 在您调用System.gc() ,您创建的SomeClass实例符合垃圾回收的条件,因为它仍然由list对象引用,即它仍然可以访问

However, as soon as this method returns list goes out of scope, so obj will then become eligible for garbage collection (as will list ). 但是,只要此方法返回list超出范围,那么obj将符合垃圾收集的条件(如list )。

Simply setting the reference obj to null does not, by itself, make the object referred to eligible for garbage collection. 简单地将引用obj设置为null本身不会使引用的对象符合垃圾回收的条件。 An object is only eligible if there are no references to it from the graph of visible objects. 只有在可见对象的图形中没有对象的引用时,对象才有资格。

will the object obj be eligible for garbage collection? 对象obj是否有资格进行垃圾收集?

Only those objects are garbage collected who don't have even one reference to reach them. 只有这些对象是垃圾收集的,甚至没有一个引用来访问它们。 (except the cyclic connectivity) (循环连接除外)

In you code, there are two reference that are pointing to new SomeClass(); 在您的代码中,有两个引用指向new SomeClass();

  1. obj OBJ
  2. zeroth index of list 第0个列表索引

You put obj = null , ie it's not pointing to that object anymore. 你把obj = null ,即它不再指向那个对象了。 But, still there exists another reference in list which can be used to access that object. 但是,列表中仍然存在另一个可用于访问该对象的引用。

Hence the object will be eligible for GC only when main returns. 因此,只有在main退货时,该对象才有资格获得GC。 ie you can't see the output of finalize method even if it got called. 即使它被调用,你也看不到finalize方法的输出。 (not sure if JVM still calls it) (不确定JVM是否仍然调用它)

不,因为该对象实际存在于列表中。

You as Java programmer can not force Garbage collection in Java; 你作为Java程序员不能强迫Java中的垃圾收集; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size 它只会在JVM认为需要基于Java堆大小的垃圾收集时触发

When a Java program started Java Virtual Machine gets some memory from Operating System. 当Java程序启动时,Java虚拟机从操作系统获取一些内存。 Java Virtual Machine or JVM uses this memory for all its need and part of this memory is call java heap memory. Java虚拟机或JVM使用此内存满足其所有需求,此内存的一部分是调用Java堆内存。

Heap in Java generally located at bottom of address space and move upwards. Java中的堆通常位于地址空间的底部并向上移动。 whenever we create object using new operator or by any another means object is allocated memory from Heap and When object dies or garbage collected ,memory goes back to Heap space in Java 每当我们使用new运算符或任何其他方法创建对象时,对象都是从Heap分配内存,当对象死亡或垃圾收集时,内存将返回到Java中的堆空间

EDIT : 编辑:

will the object obj be eligible for garbage collection? 对象obj是否有资格进行垃圾收集?

No, because the object is still in the ArrayList. 不,因为该对象仍在ArrayList中。

同意,只要它在列表中,它就不会被垃圾收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM