• 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

MindQ #36, regarding GC

 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection once execution has reached the line 9?


the answer given is 1, but i think the answer 2, here is why
1) line 2; 'newname' was assigned to object "Nick"
2) line 3; 'newname' was assigned a new value ("Jason"). making "Nick" eligible for GC
3) line 4; 'name' is assigned a value
4) line 6; 'newestName' is assigned a value
5) line 8; 'name' is assigned a new value making it eligible for GC.
Now, i know that an object is eligible when:
1) when a reference is assigned to null. line 8
2) when there is no references refering to an object. line 3
any comments?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After line 8 executes there is only one object which has no references to it because the reference contained in variable name is copied to newestName on line 6. Here's how I understand the references work in this case:
1) line 2: variable newName is assigned a reference to String "Nick"
2) line 3: variable newName is assigned a reference to String "Jason"
3) line 4: variable name is assigned a reference to String "Frieda"
4) line 6: variable newestName is assigned to refer to the same String as name, "Frieda"
5) line 8: variable name's reference is nullified. How ever the variable newestName's reference is as it was.
The only object that has lost all references to it by now is the String object "Nick".
Hope I made sense to you
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Per the above code, at th end of line # 8, newName still refers "Jason", newestName refers "Fried" which was assigned at line # 4, and the only object that is eligible for GC is "Nick".
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vicken,
According to me, the answer is 1.
Actually itz the objects that are garbage collected and not the variables.

In the above code there are 3 objects created
1)String object "Nick"
2)String object "Jason"
3)String object "Frieda"
At the end of line 9 you can still access "Jason" by variable-newName
and also access "Frieda" by the variable-newestName
but you cannot access "Nick" by any variable, so it becomes eligible to GC. And the other two still exist and are not elible to Garbage collection.
[ September 18, 2003: Message edited by: shweta mathur ]
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, till the time I answered the question 2 more similar answers came up...
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooops, how the hell i missed that!!! it so obvious
you are all correct guys. The problem is that I just misread the code.
These minor reading errors� I should be more careful when reading questions.
Anyways, thanks all for clarifying.
 
reply
    Bookmark Topic Watch Topic
  • New Topic