• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in garbage Collection Pls clarify

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks every one

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

any ways
clarified.

cheers
Deepika
 
reply
    Bookmark Topic Watch Topic
  • New Topic