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??
Sagar
Steve Simon Joseph Fernandez
Ranch Hand
Joined: Jul 17, 2005
Posts: 35
posted
0
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
Joined: Aug 20, 2001
Posts: 1815
posted
0
"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.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
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.
JavaBeginnersFaq "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
posted
0
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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.