简体   繁体   中英

why the following object is not eligible for garbage collection?

In the following code below ,I didn't understand why the object created with reference b2 is not null ??. As I have made the b2 = null by passing it to the go() method of the b1 's object(before making the b1 = null ).

class Demo {

    Short story = 200;

    Demo go(Demo cb) {
        cb = null;
        return cb;
    }

    public static void main(String[] args) {
        demo b1 = new demo();
        demo b2 = new demo();
        demo b3 = b1.go(b2);
        b1 = null;

        // b1 has been collected by gc hence is null
        System.out.println(b1.hashCode());

        // b3 is also null
        System.out.println(b3.hashCode());

        // but how come b2 is not null?
        System.out.println(b2.hashCode());

    }

}

So please someone explain me why b2 was not garbage collected even making it null?

demo b3 = b1.go(b2);

Demo go(Demo cb){
    cb = null;
    return cb;
}

With the above code, the First both b2 and cb will be pointing to the same object. Then when you do the cb = null; then the cb , reference is lost. But b2 is still pointing to the original object.

Which mean that at least one object ( b2 ) is actively holding the reference to the allocated memory/object. It will not be eligible for garbage collection.

Setting any object to null reference does not mean that it is collected by garbage collector. It just mean that, now there are no active reference to the allocated object, so it is eligible for garbage collection.

You first need to know that garbage collection (GC) is not the same as setting something to null.

Let me do an analogy here. The variables ( b1 , b2 , b3 etc) are like children. And the objects ( new Demo() ) are like balloons. When you write:

demo b1 = new demo();

You make the child b1 hold a new balloon. When you set b1 to null, you make the child let go of his balloon. The balloon flies away, but it still exists somewhere in the sky. On the other hand, GC is like a super hi-tech machine that is able to catch these flying balloons. After the child let go of the balloon, GC notices it after some time (this "time" may vary) and goes to collect it and destroys it.

Now that you learned the difference between nullity and GC, we can go on to your real problem. In the method

Demo go(Demo cb){
    cb = null;
    return cb;
}

You are setting cb to null and returning it. The return value is null. However, cb is not the child you passed in earlier ( b2 ). You have a child who is holding a balloon ( b2 ) and when you "pass the child in as a parameter", you are not passing the child, you are inviting another child to hold on to the same balloon. In the method, cb is the another child. So when set cb to null, you are letting the "another child" let go of the balloon, but the original child b2 is still holding it.

See? b2 is still holding the balloon! He is not null!

Because cb points to the same object that b2 does, but cb is NOT the same variable as b2. Setting cb to null does nothing to b2 or to the object pointed to by b2.

That is not how garbage collection works. The garbage collector will find objects that no longer have any references to them, and collect them. It will not collect objects that still have references to them, and it will not set any variables to null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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