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

garbage collector problem

 
Sagar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) public void method(){
2) String a,b;
3) a=new String("hello world");
4) b=new String("game over");
5) System.out.println(a+b+"ok");
6) a=null;
7) a=b;
8) System.out.println(a);
9) }
In the absence of compiler optimization,which line is the earliest

point the object a refered is definitely elibile to be garbage

collected?

can somebody please explain?Is 6 the correct answer??
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, 6 is the correct answer; it is at that line that you're releasing the reference held by a.

_steve.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"piyush"

Welcome to Java Ranch! We don't have too many rules around here, but please do take a moment to read our JavaRanch Naming Policy. We rather insist on the use of both a first and a last name (and ones that are not obviously fictitious, at that). Please change your display name to comply.

Thanks.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush.S,
Thank you for responding to Joel's request. Unfortunately, your name still does not comply. You need a firstName (or first initial), a space, and a lastName (not a last initial). Thanks again for your co-operation.
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piyush,
Totally, JVM will create 6 objects in the memory and out of 6 only one object is eligible for garbage collection that too object referenced by variable a. But the value it points "game over" object will not be released since it has the reference from String constant pool table.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic