• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection Question from VoodooExam

 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


after line 9 execute, how many objects are eligible for garbage collection?

Options :

a . 0
b . 1
c . 2
d . 3
e . 4

I was confident that the answer is A.But it says the answer is C:2 ...please help me out here....
[ January 10, 2008: Message edited by: sugantha Jeevankumar ]
 
Alejandro Galvan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sugantha:

Take a look carefully in your code, in line 4 you're creating a new object and refering it with gc2, on line 5 you're creating a second object and refering it with gc3, on lines 6 and 7 you are crossing references, I mean you just are doing that both gc inside your two objects refer to each other, but in line 8 you are creating a new object, this object has its own gc, but it refers to NULL and you are refering it with gc2, finally you are refering this last object which its gc referes to NULL with gc3.

So both reference variables (gc2 and gc3) points to the same object (the one which its gc refers to NULL), the other two objects have no reference alive, you can think "but object1.gc refers to object2 and object2.gc refers to object1, so they still having references alive", the answer is no, since you have no an alive reference variable from your thread which refer to object1 there is no way to access object2 throught object1.gc and viceversa, so both objects have lost their connection to the world, indeed they are elegibles for garbage collector.

Do you get it? I hope this help you to understand it.

Also I want to encourage you to take a look around, there are so many cases about garbage collector in JavaRanch, some of them are quite similar to your doubt.

Regards,
 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Alejandro...that was my oversight.Thanks for clearing my doubt
 
Alejandro Galvan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't apologize, we all are here to learn and to ask each others; if don't, I would not answer you, I glad that I can help you. Just take a bit time to read other's questions, it helped to understand some things.

Take care

Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic