Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a garbage collection question from K&B self-test

 
Emma Yu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giving the following,
12. X3 x2=new X3();
13. X3 x3=new X3();
14. X3 x5 = x3;
15. x3=x2;
16. X3 x4=x3;
17. x2 =null;
18. // insert code
what two lines of code, inserted independently at line 18, which make an object eligible for garbage collection?
A. x3=null;
B. x4=null;
C. x5=null;
D. x3=x4;
E. x5 = x4;
answer is C and E. Why? which object is eligible for garbage collection in this case? Why not A? if x3 =null, then x2 is eligiable for collection, right? I am very confused about garbage collection. please advise.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emma,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
C. Nimo
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
When you look at garbage collection, you need to think which objects are being created, and when objects are no longer being pointed to by anything.
In the first two lines, the code creates two new X3 objects, lets call them a and b.
If you follow the code, you see that by the time you reach line 17, your new object a is referenced by x2,x3 and x4, while the object b is only referenced by x5.
Therefore, if you change x5 to reference a different object, no one references object b anymore - and it will become elligible for collection.

Hope this helps.
Nimo
 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emma, lets go one by one statement
12. X3 x2=new X3();
First object is cretated and x2 is pointing to it
13. X3 x3=new X3();
Second Object is created and x3 is pointing to it
14. X3 x5 = x3;
Now x5 is also pointing to the second object.
So right now we have two objects, one is referenced by x2 and the second object referenced by x3 and x5
15. x3=x2;
Now x3 is also pointing the same object as x2.
What this Means is now we have two objects, one is referenced by x2 and x3 and the second object referenced by x5

16. X3 x4=x3;
Now x4 is also pointing the same object as x3.
What this Means is now we have two objects, one is referenced by x2, x3 and x4 & the second object referenced by x5
17. x2 =null;
Now x2 is not pointing any object
What this Means is now we have two objects, one is referenced by x3 and x4 & the second object referenced by x5
18. // insert code

Now to make the first object eligible, we have to set both x3 and x4 to null.
To make the second object eligible, we have to set x5 null.

A. x3=null;
After this statement x4 will be pointing to the first object and x5 to second. so no objects eligible for garbage collection

B. x4=null;
After this statement x3 will be pointing to the first object and x5 to second. so no objects eligible for garbage collection
C. x5=null;
After this statement x3 and x4 will be pointing to the first object and none to second. so second object can be garbage collected (ANS)
D. x3=x4;
This won't make any difference. This is how they currently are. Both x3 and x4 pointing to the same
E. x5 = x4;
After this statement x3, x4 and x5 will be pointing to the first object and none to second. so second object can be garbage collected (ANS)
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion for garbage collection is to draw pictures. If you can draw a picture, you can answer any question about garbage collection.
First of all, keep in mind that an object is eleigible for garbage collection when there are no active references to it. Let's go through this code line by line and draw pictures to keep track of what's going on, shall we?

As you can see from our picture, we now have a variable named x2 which references an object of type X3. Let's go on to the next line of code and update our picture.

Now we have a variable named x3 which references another X3 object. Moving on...

Now we have two variables, x3 and x5, referencing the same X3 object.

Now, x3 references the same object x2 does but, because x5 references the other object, nothing is yet available for collection.

We now have three variables all referencing the same X3 object.

So, at this point, x2 references nothing, but we still have active references to both of our objects. Now, let's go through the various choices to see what each one would do to our picture. If any of those choices modifies our picture so that we no longer have a reference to an object, we have a case in which an object is eligible for garbage collection. We'll start with the first option:

Well, with that option, we still have active references to everything, so that one's not right. With the next option:

Nope - we still have active references to both objects so no dice. Next option...

Aha! Jackpot! In this case, no variables reference the second X3 object, so that object is now eligible for garbage collection. So, selection C is correct. But, the question asked for 2 answers, so we must go on...

Heck, x3 and x4 already referenced the same object, so this didn't change anything at all. Obviously, nothing is eligible for garbage collection here. Let's check out the final option:

Another winner! By changing x5 to reference the first of the two objects, we're left with no active references to the second X3 object.
So, after all that hard work, we're left with C & E as the correct answers.
I realize this was a very long-winded answer to your question, but I wanted to not just lay out the answer for you, but lay out a method for finding the answer. If you ever run into a garbage collection question again, draw a picture - if you can do that, you can answer the question.
I hope that helps,
Corey
 
Emma Yu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all of you very much. You give me very good and detail explanations. I think now I understand much better about what objects are eligible for garbage collection.
 
Emma Yu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plus, Corey, the diagrams you drew are very nice and make a lot senses to me. I think K&B should include such kind of pictures in their book. This definitely helps. I am now much more confident for garbage collection questions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic