• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q29 from javaprepare.com
29.At what stage in the following method does the string "abc" becomes available for garbage collection. Select the one correct answer.

A.Before statement labelled 1
B.Before statement labelled 2
C.Before statement labelled 3
D.Before statement labelled 4
E.Never.
The answer is D. Should it not be C ? i.e. Before stmt 3.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur,
The same question has been discussed here

Ajith
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! Need to be more regular at the ranch, I guess!!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void method X()
{
String r = new String("abc");
String s = "abc";
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
Hi ankur which String u r talking about is the String present in Heap memory that was created using statement String r = new String("abc");
Well if this is the string u r expecting it would be eligible for garabage collection at anytime after line marked with //1 is executed.
if u r talking about the String created in the String pool with
String s = "abc" ..Then it would be available for garbage collection only after class in which this method is declared is unloaded.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic