File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes garbage collector problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "garbage collector problem" Watch "garbage collector problem" New topic

garbage collector problem


Joined: Mar 25, 2005
Posts: 14
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


can somebody please explain?Is 6 the correct answer??

Steve Simon Joseph Fernandez
Ranch Hand

Joined: Jul 17, 2005
Posts: 35

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

Joel McNary

Joined: Aug 20, 2001
Posts: 1824


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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
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.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
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.
I agree. Here's the link:
subject: garbage collector problem
jQuery in Action, 3rd edition