• 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

Garbage collection

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.


void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4



Before statement labeled 1
Before statement labeled 2
Before statement labeled 3
Before statement labeled 4
Never.

Can someone explain what will be the answer to this question and why ?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Answer is "Never".
To be more correct it's eligible for garbage collection after "methodX()" get's executed.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I disagree. The correct answer is "Before statement labeled 4". The reason is that "the object initially referenced by s" is a String object with the value of "abc". In statement 3, a String object is created with the value "abcnull" and that String is then assigned to s. As a result, the "abc" String that used to be referenced by s is no longer referenced by anything, so the object initially referenced by s is eligible for garbage collection as soon as statement 3 completes, which is immediately before statement 4.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sisir Chanda:
yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.



where u have read this about...
give me the linke....

i think joe sanawitz is perfectly right and i have seen that explanation every where but your statement is to be honest strange..

anyway give me the link from where you have read this..
 
Sisir Chanda
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep...It was my mistake...I read that the equestion is '...when the object refernced by s is eligible for GC...'. Since before line 4 , '....s = s + r; //3
...' was used, so my answer was s will never be eligible for GC. But the actual question was '....when the INITIAL object referenced by s will be eligible for GC.....'

The the answer is definitely....Before statement labeled 4. since at line 3 s has been assigned to a different object and s is referring to a different object. Having said that, the INITIAL object reference by s will become eligible for GC.

Thanks guys!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic