I have been a regular reader of javaranch. Thanks all for the great work. Please let me know of some good material for Garbage collection. Also tell me how this works....question from K&B QUESTION 1: X3 x2 = new X3(); X3 x3 = new X3(); X3 x5 = x3; x3 = x2; X3 x4 = x3; x2 = null; //insert code what two lines of code inserted independently at line 18 wil make an object eligible for GC. Ans: x5 = null; x5 = x4; QUESTION 2:
class A { private B b; ..............//1 void test(){ b=new B(); this.demo(b); } void demo(B bb) { bb = null; bb = new B(); } } When is the B object at 1 eligible for garbage colelction.
Thanks, Swapna.
Nicky Eng
Ranch Hand
Joined: Mar 26, 2005
Posts: 378
posted
0
for the question 2, i think the object in the indicated line 1 will not be eligible for garbage collection until all the process been done. Because it there is a method still using the object b, and eventually life time of object b is not determined.
From NickyEng
Diploma in Computer Studies
SCJP 1.4
SCWCD 1.4
Formula 1 app by Maxis (Playbook)
Arulkumar Gopalan
Ranch Hand
Joined: Oct 13, 2003
Posts: 104
posted
0
FOR Q1: -------
After the execution of above code:
- X4 & X3 references are pointing to the object created by x2(X3 x2 = new X3() ) - X5 reference is pointing to the object created by x3(X3 x3 = new X3() )
so, any one of the below statements frees an object for GC. x5 = null; x5 = x4; x5 = x3; //is this statement not given as an answer?
FOR Q2: ------- As posted above. [ August 01, 2005: Message edited by: Arulkumar Gopalan ]
Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Arulkumar Gopalan
Ranch Hand
Joined: Oct 13, 2003
Posts: 104
posted
0
For Q1: I don't think there might be a necessity to write such redundant code in practical.