Java Garbage collection, setting reference to null

淺唱寂寞╮ 提交于 2020-01-04 09:15:08

问题


public class A{
    A a;

    public static void main(String args[]){
        A b = new A();//new object created, obj1
        b.a = new A();//new object created, obj2
        b = null;
        //line 8
    }
}

When line 8 is reached, obj1 is eligible for GC. Is obj2 also eligible for GC?


回答1:


If you'd like to determine eligibility of an object for garbage collection, try to see if it is reachable from the root set. The root set are things like objects referenced from the call stack and global variables.

In your example, the root set initially consists if obj1 and args (let's ignore any others that might exist - they don't matter for your example). Just before line 6, obj2 is clearly reachable from the root set, since obj1 contains a reference to obj2. But after line 7, the only object in the root set is args. There's no way either obj1 or obj2 is referenced from args, so at line 8 both obj1 and obj2 are eligible for collection.




回答2:


The only reference you created to obj2 was within obj1 (b.a = new A();). As soon as you lost your reference to obj1 (b = null;) you also lost your reference to obj2, so yes, it is eligible for GC.




回答3:


Yes and here is an example showing GC in action:

static int c = 2;
public static void main(String args[]) throws Exception {
    class A{ 
        A a; 
    }
    A b = new A(){
        public void finalize(){
            System.out.println("obj 1 has been GC'd");
            c--;
        }
    }; 
    b.a = new A(){
        public void finalize(){
            System.out.println("obj 2 has been GC'd");
            c--;
        }
    }; 
    b = null;        
    while(c>0) {
        System.gc();
        Thread.sleep(42);
    }
}

Output:

obj 1 has been GC'd
obj 2 has been GC'd



回答4:


   b = null;

is useless, because one line later you already reach the end of scope of b. None of the 2 objects are reachable after leaving the scope where they are defined, since their reference wasn't put somewhere else, by a method call or as paramter in a constructor call, or as backreference from something else, which was published somewhere else.



来源:https://stackoverflow.com/questions/9747376/java-garbage-collection-setting-reference-to-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!