Java Garbage collection, setting reference to null

2019-07-16 04:33发布

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?

4条回答
来,给爷笑一个
2楼-- · 2019-07-16 05:11
   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.

查看更多
男人必须洒脱
3楼-- · 2019-07-16 05:16

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.

查看更多
Explosion°爆炸
4楼-- · 2019-07-16 05:18

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.

查看更多
看我几分像从前
5楼-- · 2019-07-16 05:19

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
查看更多
登录 后发表回答