aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in garbage Collection  Pls clarify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in garbage Collection  Pls clarify " Watch "Doubt in garbage Collection  Pls clarify " New topic
Author

Doubt in garbage Collection Pls clarify

Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
Here are my questions

class X2
{
public X2 x;
public static void main(String args[])
{
X2 x2= new X2(); //1
X2 x3= new X2(); //2
x2.x = x3; //3
x3.x = x2; //4
x2 = new X2(); //5 What is happening at this point
x3 = x2 ; //6
doStuff();
}
}


so how many objects are eligible for garbage collection by line 6 (I mean line correspnding //6)

answer is two ie bot x2,x3
I didnot get whats happening at 5,6 x2 is instantiated again ...
pls explain me thos two line //5 , //6




I also have other query

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, will make an object eligible for garbage collection?(select two.)

a) x3 = null;
b) x4 = null;
c) x5 = null;
d) x3 = x4;
e) x5 = x4;


solution for this c, e

I just could not understand how is it going please someone kindly explain me clearly

thanks

Deepika Namasani


Deepika N
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Deepika,
In the first case,"island of isolation" takes place....i.e

Here we create two objects and made both of them to point to each other.
Now next,

We made the reference variable x2 to point to a new object,and make x3
reference the same new object as x2.So,the two initial objects created are
eligible for garbage collection under "island of isolation" scenario.K&B book has a full article on this.

In the second case,

say,x2 is pointing to "object 1" and x3 is pointing to "object 2",on line
14,x5 is made to point to "object 2" as well,now,on line 15,x3 is made to
point "object 1",line 16,make x4 point to "object 1" as well,on line 17,x2
reference is removed.So,at this point "object 1" is pointed by x3,x4 and "object 2" ispointed by x5.Now,you will get why "x5=null" and
"x5=x4" are the answers...rite

Hope this helps..
thanks


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
First, if you post code please put the code in code tags (there is a button below where you post).

Second, I would like to shoot whoever came up with the examples for the mind bending class and variable names. Now lets try to answer this.


The following happens:
Lets call the X2 created at line 1 object1, the X2 created at line 2 object2, and the X2 created at line 5 object3

there is no longer anything pointing to object1 or object2

That may help you with the second one also, if I have time later I'll post the steps on that one if somebody doesn't beat me to it.
Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
Hello thanks for making the concept clear, but i have a small confusion with second question








according to Jas and Steven explanation this is how i understood


followin is the question
what two lines of code inserted independently at line 18, will make an object eligible for garbage collection?(select two.)

a) x3 = null;
b) x4 = null;
c) x5 = null;
d) x3 = x4;
e) x5 = x4

so
at line 17
Obj 1 has still x3 and x4 references on it
and Obj 2 has x5 referance

so i agree making x5= null would make Obj 2 referances free and is eligible for garbage colletion also redirecting x5 to Obj1 ie by refering to x4 Obj2 referance free

but in that case y not selecting (a)&(b) wouldnot be the correct answers
if x3= null and x4 = null Obj1 would be referance free and will be available for garbage collection.

If my way of understanding the concept is clear should (a) & (b) are they not right answers
This question is from K&B selftest only and i dont think they will give wrong answers

i am just confused for this pls explain

thanks
Deepika
[ May 25, 2005: Message edited by: Deepika Namasani ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
The question want the two lines that when each used alone will cause an object to be elegible for gc, not two lines that used together can.
swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
dipika ,u have understood the concept right but question is about selecting any two independent statement that will make object available for garbage collection.
Making x3=null and x4=null will make object available for garbage collection
But using one of the x5=null or x5=x4 will make object available for garbage collection.
hence the answer.
Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
Thanks every one

may be i should slow down while reading a question or re read it .

any ways
clarified.

cheers
Deepika
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in garbage Collection Pls clarify