Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection question

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point is the object CREATED AT LINE 05 available for garbage collection.



Select most appropriate answer
a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 9
f) At line 12

My doubt is the answer option d or e is correct. Kindly help.
When the control comes to execute line 09,the anObj object reference started to point to new object SAMPLE.So I am asking is the answer e is correct.
[ September 27, 2006: Message edited by: Shiva Mohan ]
 
Chris Goad
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E looks good to me. At that point, your variable would be referring to a new object since Strings are immutable.

Chris
 
Pedro Lobato Sena
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"E" appears to be the right answer for me too.
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since the strings are immutable thats why a new object is created by this line
anObj = anObj.UpperString();

but my question is control cannot go back ...
only when the obj is assigned null then only it is valid entry for the GC..

please can anyone explain me..

thnx
Anvi
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

if the object has no references from any live thread,then it is eligible for the garbage collection.

when
anObj = anObj.toUpperCase();
is executed a new string object will be created and the reference anObj is pointing to it.But the old string doesn't have any references assigned to it. so it is eligible for garbage collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic