Naman
Naman

Reputation: 2373

Java Garbage collection, setting reference to null

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?

Upvotes: 2

Views: 573

Answers (4)

user unknown
user unknown

Reputation: 36269

   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.

Upvotes: 0

Java42
Java42

Reputation: 7716

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

Upvotes: 1

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19347

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.

Upvotes: 3

Jeffrey
Jeffrey

Reputation: 44808

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.

Upvotes: 5

Related Questions