-2

I'm little bit confused to find out how many objects are eligible for GC when line 18. Plz explain

class B{}
class A{
    static B b1;
    B b2;
}
public class Test {
    public static void main(String[] args) {
        B b1=new B();
        B b2=new B();
        A a1=new A();
        A a2=new A();
        a1.b1=b1;
        a1.b2=b1;
        a2.b2=b2;
        a1=null;
        b1=null;
        b2=null;
        // TODO Auto-generated method stub
    }
}

How many objects are eligible for GC when line 19

  • 1
    depends on which line IS line 19 ;) – Mark Dec 27 '16 at 09:45
  • 1
    Assuming line 19 is the last line (i.e. the closing bracket) of `main`, all objects are eligible for GC since this is the end of the program. How many those are I cannot tell exactly since I do not know exactly which classes get loaded by the JVM. After `a1 = null;`, only `a1` is eligible for gc. After `b1 = null;`, `b1` gets eligible for GC. `b2` does not get eligible for GC after `b2 = null;`, since it is still reachable through `a2.b2`. – Turing85 Dec 27 '16 at 09:45
  • Refering to my former comment: please note that when I worte "`a1` gets eligible for GC", I actually meant "the object, `a1` referenced before it was set to `null`, gets eligible for GC". It is hard to talk about this object since it is existent and nameless as long as it is not garbage-collected. I hope the intended meaning is clear. – Turing85 Dec 27 '16 at 09:54
  • Until you tell us which line is line 19, this question is unanswerable. – Dawood ibn Kareem Dec 27 '16 at 10:07

2 Answers2

1

Assuming that line 19 is the line marked by TODO Auto-generated method stub, then of the objects created by the program the only object which is eligible for GC by line 19 is the object referred to by the variable a1.

The object which was originally assigned to a2 is still assigned, so it's not available.

The static value in A.b1 is still assigned to the object which was originally assigned to b1 so that object is not available for GC - static values live for the lifetime of the class so even though instance a1 may get GC'd the static instance which was assigned through it can still be referenced.

Because instance a2 is still assigned, the instance field a2.b2 is also unavailable for GC. This value holds the instance of B originally assigned to local variable b2 in the main method.

Of the four objects created in the main method the only one actually available for GC by the final line of the program is the object initially assigned to a1. It's only reference has been set to null and so nothing the instance is not reachable anymore. All other instances are reachable either via a local variable (a2), a static reference (A.b1) or an instance reference (a2.b2).

sisyphus
  • 6,174
  • 1
  • 25
  • 35
1

Green objects are eligible for garbage collection.

As soon as an object is eligible for garbage collection, references from within that object don't matter anymore.

However, static references don't actually need an object to exist (the A without the : on the right is meant to show the static reference b1).

:A represents an object of the class A

:B represents an object of the class B

enter image description here

Mark
  • 1,498
  • 1
  • 9
  • 13