Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

garbage collector exaple

At what point will the object created on line 8 be eligible for garbage collection?

public class RJMould{
    StringBuffer sb;
    public static void main(String argv[]){
   RJMould rjm = new RJMould();
   rjm.kansas();
   }
    public void kansas(){
   sb = new StringBuffer("Manchester");
   StringBuffer sb2 = sb;
   StringBuffer sb3 = new StringBuffer("Chester");
   sb=sb3;
   sb3=null;
   sb2=null;
    }
}


1) Line 11
2) Line 9
3) Line 12
4) Line 13

I was trying above example from link
http://www.jchq.net/certkey/0301certkey.htm

did not understand THE ANSWER.It said line 13. I was thinking line 11 as that particular line sb is assigned to  sb3.

I was trying to understand the answer.


 Any links, ideas, resources,sample code highly appreciated. thanks in advance.








Which of the following statements are true?

1) finalize will always run before an object is garbage collected
2) finalize may run before or after an object is garbage collected
3) finalize will run when an object becomes unreachable
4) finalize allows a programmer to free memory allocated to an object

Finalize will always be run before an object is garbage collected. It cannot run after it is collected because by then the object will cease to exit. When an object becomes unreachable it will be eligible for garbage collection but there is no guarantee when finalize will run, only that it will run before garbage collection happens. The final option is a passable description of destructors in C++ but not of the finalize method in Java.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> did not understand THE ANSWER.It said line 13. I was thinking line 11 as that particular line sb is assigned to  sb3.

sb3 (and sb2) will *reference* the same object referenced by sb
It will not be available for gc until there are no references to the object
SOLUTION
Avatar of msk_apk
msk_apk
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gudii9

ASKER

>>>sb3 (and sb2) will *reference* the same object referenced by sb

you mean sb and sb2?

I was thinking those two refer to Mancheser stringbuffer object where as sb3 point to Chester stringbuffer object.
>>it creates one StrongReference object sb2 and it points to the StringBuffer("something") in the heap.
does sb drops or disconnected  pointing to StringBuffer("something") in the heap. at this point.
What is meaning of StrongReference object.  will there be weak StrongReference object also.
>>StrongReference object sb2 is garbage collected.
you mean eligible for garbage collection or garbage collected?


 May be if you can send small picture or graphical representation of how to understand this concept that would be great. please advise
sorry yes


   sb = new StringBuffer("Manchester");

sb -> Manchester

   StringBuffer sb2 = sb;

sb -> Manchester
sb2 -> Manchester

   StringBuffer sb3 = new StringBuffer("Chester");

sb -> Manchester
sb2 -> Manchester
sb3 -> Chester

   sb=sb3;

sb -> Chester
sb2 -> Manchester
sb3 -> Chester
Avatar of gudii9

ASKER

>>>

   sb=sb3;

sb -> Chester
sb2 -> Manchester
sb3 -> Chester


i understood till here. But how making sb2=null makes sb null at line 13 please advise
it doesn't
Avatar of gudii9

ASKER

so how the answer is line 13 which basically asks
At what point will the object created on line 8 be eligible for garbage collection?
please advise
Avatar of gudii9

ASKER

sb2=null,

does it null sb2 object reference on stack or Manchester String Buffer object on heap.


when sb2=null does that means sb=null automatically?

please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.