4 Replies Latest reply: Jun 30, 2013 11:59 PM by Pradeep RSS

    garbage collection


      Hi, I am going through SCJP material and I have a doubt regarding a question I came across in that material:


      3. class Beta { }

      4. class Alpha {

      5. static Beta b1;

      6. Beta b2;

      7. }

      8. public class Tester {

      9. public static void main(String[] args) {

      10. Beta b1 = new Beta(); Beta b2 = new Beta();

      11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();

      12. a1.b1 = b1;

      13. a1.b2 = b1;

      14. a2.b2 = b2;

      15. a1 = null; b1 = null; b2 = null;

      16. // do stuff

      17. }

      18. }

      When line 16 is reached, how many objects will be eligible for garbage collection?

      A. 0

      B. 1

      C. 2

      D. 3

      E. 4

      F. 5


      ® ✓ B is correct. It should be clear that there is still a reference to the object referred to by

      a2, and that there is still a reference to the object referred to by a2.b2. What might be

      less clear is that you can still access the other Beta object through the static variable

      a2.b1—because it's static.


      For this question I thought the answer would be 4 at first, since a1 is declared NULL. This a1 and a2 has two other objects b1 and b2, which are made to point to the other two objects b1 and b2. But they are also declared NULL, so I thought a1, a1's b1, b2, a2's b2 will also become NULL.


      Please help me understand this answer B.



        • 1. Re: garbage collection

          Yes the answer is B alright.

          Here is how it is :

          Let us say A1 is the object that is referenced by a1, A2 is the one referenced by a2. Similarly, B1 for b1 and B2 for b2

          a1.b1=1 and a2.b1=1 are static fields. so even when a1=null, a2.b1 holds a reference to B1

          And a2.b2=b2 holds the reference to the object B2.

          So only A1 has no reference ( A2 is referenced by a2).


          Thus only A1 is eligible for garbage collection

          • 2. Re: garbage collection

            Hi Pradeep, Thanks for your reply


            Here the a2.b1=b1, is not assigned in the code. But still since a1.b1 is static and is made to point to b1, the object of B1, are you saying that the b1 will stay alive until the class ends?


            I can understand the reason why the object of b2 will stay alive, since a2.b2 is made to point to it.




            • 3. Re: garbage collection

              Code is hard to read.  Following is broken down with comments that explain each reference.

              class Beta { }
              class Alpha 
                  static Beta b1;   // Reference X1
                  Beta b2;          // Reference X2
              public class Tester {
                  public static void main(String[] args) 
                      Beta b1 = new Beta();   // Reference Y1 - Instance 1
                      Beta b2 = new Beta();   // Reference Y2 - Instance 2
                      Alpha a1 = new Alpha();    // Reference Y3 - Instance 3
                      Alpha a2 = new Alpha();    // Reference Y4 - Instance 4
                      a1.b1 = b1;   // Reference X1 set to Instance 1
                      a1.b2 = b1;   // Reference Y3.X2 set to Instance 1
                      a2.b2 = b2;   // Reference Y4.X2 set to Instance 2
                      a1 = null;    // Reference Y3 clear
                      b1 = null;    // Reference Y1 clear
                      b2 = null;    // Reference Y2 clear
                      // At this point there are 4 instances.
                      // Result
                      // Y4 is still set
                      //   Thus Instance 4 is still referenced
                      //   Thus Y4.X2 still set
                      //      Thus Instance 2 is still referenced
                      // X1 is still set
                      //   Thus Instance Instance 1 is still referenced.
                      // In the above 3 instances are still referenced
                      // Thus there is one instance, Instance 3, which
                      // is eligible.
                      // do stuff
              • 4. Re: garbage collection

                yes that's right... the class memory is shared by all of its objects. so a1.b1 is the same as a2.b1 .