Jagdeep Jagdeep - 1 year ago 53
Java Question

How Many Objects Are Eligible For Garbage Collection?

1. public class Tahiti {
2. Tahiti t;
3. public static void main(String[] args) {
4. Tahiti t = new Tahiti();
5. Tahiti t2 = t.go(t);
6. t2 = null;
7. // more code here
8. }
9. Tahiti go(Tahiti t) {
10. Tahiti t1 = new Tahiti(); Tahiti t2 = new Tahiti();
11. t1.t = t2; t2.t = t1; t.t = t2;
12. return t1;
13. }
14. }


When line 7 is reached, how many objects are eligible for garbage collection?

as per the answer given for this question, there is no object eligible for GC at line 11;
but according to me at least one object, t2, which is set to point null at line 6, should be eligible for garbage collection.

Answer Source

The variable t2 on line 6 is not the only reference to the object. There is a reference from t to the object t2 which was created in the function go which in turn holds a reference to t1 which is the same object that was returned by the function go. So line 6 merely reduces the number of references but there still are live references to the object.

EDIT: Let's try a more elaborate explanation, first I reworked the code a bit to make explaining easier. One statement per line and less confusing variable names + I identified the three relevant objects with the letters A, B and C.

1.  public class Tahiti {
2.      Tahiti u;
3.      public static void main(String[] args) {
4.          Tahiti t = new Tahiti(); // object A
5.          Tahiti t2 = t.go(t);
6.          t2 = null;
7.          // more code here
8.      }
9.      Tahiti go(Tahiti s) {
10.         Tahiti s1 = new Tahiti(); // object B
11.         Tahiti s2 = new Tahiti(); // object C
12.         s1.u = s2;
13.         s2.u = s1;
14.         s.u = s2;
15.         return s1;
16.     }
17. }

On line 4: variable t is initiliazed to reference a new object. Let's call that object itself A (it normally has no name in java but for the sake of this explanation it is easier if it does).

On line 5: t is passed to the function go so we go to line 9

On line 9: the parameter s references the object A which was created on line 4

line 10: initializes variable s1 to point to object B

line 11: initializes variable s2 to refer to object C

line 12: s1.u is set to refer to s2 which means object B gets a reference to C

line 13: s2.u is set to refer to s1 so object C get's a reference to B

line 14: s.u is set to refer s2 which means object A get's a reference to C note that C also has a reference to B so at this point there is a chain from A to B

line 15 return object B and return to line 5

line 5: t2 is set to reference object B (B is now referred to two times once directly by t2 and once because t refers to object A which refers to C which refers to B)

line 6: the reference t2 is set to null so B looses one reference but t is still live which point to A refers to C refers to B