Win a copy of Design for the Mind this week in the Design forum!

# help on garbage collection (K &B's book Q)

Rob Kiu
Greenhorn
Posts: 15
Hi everyone,
Don't quite understand the explanation of the answer. Appreciate if anyone can help explain the answers in a little more detail, eg what happen as each line of the code execute....

Self Test Chap 7, Q14 K&B's book
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
Q: What two lines of code, inserted independently at line 18, will make an object eligible for garbage collection? (Choose two.)
A. x3 = null;
B. x4 = null;
C. x5 = null;
D. x3 = x4;
E. x5 = x4;
Answer: C and E.
Explanation: By the time line 18 is reached, x2 is null, x3 and x4 refer to the object created in line 12, and x5 refers to the object created in line 13. Any kind of redirection of x5 will leave the second object without a reference.
A, B, and D are incorrect because the first object has two references; changing one of the references will not cause the first object to become unreachable.
Thks a million for your help !!

Rob Kiu
Greenhorn
Posts: 15
My own thinking on the Q, correct me if I am wrong, please...
12. X3 x2 = new X3(); //object new X3()-1 reference to x2
13. X3 x3 = new X3(); //object new X3()-2 reference to x3
14. X3 x5 = x3; //x5 refer to x3 refer to new X3()-2 from L13.
15. x3 = x2; //x3 refer to x2 refer to new X3()-1 from L12.
16. X3 x4 = x3; //x4 refer to x3 refer to new X3()-1 from L15.
17. x2 = null; //set x2 ref variable to null,
//new X3()-1 lost reference, eligible for GC
18. // insert code
A. x3 = null; //if x3 ref variable set to null, cause new X3()-1
//lost reference, from L15 (same effect as L17)
B. x4 = null; //if x4 ref variable set to null, cause new X3()-1
//lost reference, from L16 (same effect as L17)

C. x5 = null; //if x5 ref variable set to null, cause new X3()-2
//lost reference, (from L14)
D. x3 = x4; //x3 refer to x4 refer to new X3()-1 also (L16)
E. x5 = x4; //x5 refer to x4 cause x3 & thus new X3()-2 to lost
//reference (L14)
Sounds convoluted, no wonder I am confused...
Anyone can explain above more clearly or simply?
Thks!

Tom Cockerline
Greenhorn
Posts: 7
Hi Rob,
Here are my annotations:
12. X3 x2 = new X3();
Line 12 creates a reference variable named x2 of type X3. Then it creates a new object of type X3. (To help keep track of the objects that get created in this code snippet, let's call this new object "ObjectOne".) Finally, it references ObjectOne with the reference variable x2. We'll keep track of reference variables and the objects they refer to with this unofficial shorthand:
x2 --> ObjectOne

13. X3 x3 = new X3();
Line 13 is a lot like line 12. It creates a reference variable named x3 of type X3. Then it creates a new object of type X3. (Let's call this new object "ObjectTwo".) Finally, it references ObjectTwo with the reference variable x3.
x2 --> ObjectOne
x3 --> ObjectTwo

14. X3 x5 = x3;
Line 14 creates a reference variable named x5 of type X3 and gives it the same object reference that x3 has. What's that? Well, up in line 13 x3 was told to refer to ObjectTwo, so that's what x5 gets too.
x2 --> ObjectOne
x3 --> ObjectTwo
x5 --> ObjectTwo

15. x3 = x2;
Now x3, which was told to reference ObjectTwo up in line 13, is told to change its reference to whatever x2 is referencing. That would be ObjectOne, from line 12.
x2 --> ObjectOne
x3 --> ObjectOne
x5 --> ObjectTwo

16. X3 x4 = x3;
A new reference variable named x4 of type X3 is created, and told to reference whatever x3 is referencing.
x2 --> ObjectOne
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> ObjectOne

17. x2 = null;
Change x2 so it doesn't reference anything. Notice that the only reference variable referring to ObjectTwo is x5, so if x5 is changed, ObjectTwo will have no live references and will thus be eligible for garbage collection.
x2 --> null
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> ObjectOne

18. // insert code
Q: What two lines of code, inserted independently at line 18, will make an object eligible for garbage collection? (Choose two.)
In other words, we've created two objects. What can we do so that one of them will be left with no live reachable reference variable?
A. x3 = null;
x2 --> null
x3 --> null
x5 --> ObjectTwo
x4 --> ObjectOne
Since x4 is still referencing ObjectOne, this doesn't do it.

B. x4 = null;
x2 --> null
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> null
Since x3 is still referencing ObjectOne, this doesn't do it.

C. x5 = null;
x2 --> null
x3 --> ObjectOne
x5 --> null
x4 --> ObjectOne
This leaves ObjectTwo without any live references. Eureka!

D. x3 = x4;
x2 --> null
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> ObjectOne
This doesn't really change anything, since x3 and x4 were already referencing the same object, ObjectOne.

E. x5 = x4;
x2 --> null
x3 --> ObjectOne
x5 --> ObjectOne
x4 --> ObjectOne
This leaves ObjectTwo without any live references, since x5 has been changed to reference ObjectOne. Double eureka!!

Hope that helps.
Tom Cockerline

Rob Kiu
Greenhorn
Posts: 15
Hi Tom,
Double or should I say triple eureka indeed !!
Your explanation is super, the Q and A seems crystal clear to me now.
Thanks very much!

Cheers,
Kiu

Vineela Devi
Ranch Hand
Posts: 191
Hi Rob,
In this code at line 12 u r creating a new object( let's say object1) with the refernece variable x2.

X3 x2 = new X3(); //let's say object 1 is created with the reference variable x2
at line 13 u r creating another new object( let it be object 2) with the reference variable x3.
X3 x3 = new X3(); // object 2 is created with the refrence variable x3.
At line 14 u r craeting a new object ref x5, but not creating a new obj rather refering to the same object as that of x3.
X3 x5 = x3; // x5 is refering to the object created at line 13 ( i.e the object referred by the ref variable x3)
at line 15 u r assigning x2 to x3. i.e u r changing the object referred by x3. noe x3 doesnot refer to object 2 rather it refers to object1 created at line 12.
x3 = x2; //x3 refering to obj reffered by x2.
So, by the end of line 15 there r two references to object1 i.e. both x2 and x3 refer to object 1 and only refernce to object 2. i.e only x5 referring to object2( x3 no longer refers to obj2).
at line 16 new refernce variable x4 is cretaed and is reffering to obj reffered by x3.i.e object 1.
X3 x4 = x3; //x4 is created and it refers to obj reffered by x3.i.e. object 1 now.
so, by the end of line 16 object 1 has three refferences(x2,x3,x4) and obj 2 has only one(x5).
at line 17, u r assigining null to the refernec variable x2. i.e it no longer refers to any object.
x2 = null; // x2 no more refers to obj1
so, by the time line 18 is reached object 1 has only two refernces (x3and x4) and object 2 has one ref(x5).
we know tht for an obj to become eligible for garbage collection , there should be no ref to it.
i think its clear upto this point.
coming to the q and going thru the choices given

in the q if they had not mentioned the term independent, then u can go for 1 and 2 option.i.e if u set both x3 and x4 to null which r reffering to obj 1 then obj1 becomes eligible for garbage collection.But , these two r dependent on each other.if u set x3 to null and leave x4,or vice versa then obj1 will not be eligible for garbage collection coz there still exists a refernce to obj 1.
but as they had specified the term independent, 1 and 2 are not valid choices.
now coming to option 3 it is true since there exists only one ref to obj2 when line 18 is reached and so by assiging null to x5 obj2 becomes eligible for gc.
option 4 is not valid since making x3 refer to the obj reffered by x4 doesnot make any diff since already x3 is reffering to the same obj.
option 5 is correct coz making x5 refer to the object referred by x4 will leave no ref to obj 2 and hence obj 2 becomes eligible for gc.
hope this helps u.
vineela

Rob Kiu
Greenhorn
Posts: 15
Dear Vineela,
Thanks a whole bunch for ur explanation. Helps a lot!! Feel I can tackle such gc questions now without problem.
Cheers,
rob

Bert Bates
author
Sheriff
Posts: 8898
5
I could say that we love causing so much pain
But really, we don't, we just like to toughen you up for the real exam!
Bert
p.s. for some reason, the GC questions are always amoungst the toughest you'll encounter, so study GC thoroughly!