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.
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
> 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
0
msk_apkCommented:
yes it is at line 13. when you do StringBuffer sb = new StringBuffer("something"), it creates two objets. One StrongReference object (sb) in the stack and one StringBuffer() object in the heap.

when you do

StringBuffer sb2 = sb;

it creates one StrongReference object sb2 and it points to the StringBuffer("something") in the heap. when you say

sb2 = null;

StrongReference object sb2 is garbage collected. when no reference object is referring to StringBuffer("something") then it is eligible for garbage collection. At line 13, method ends, scope of that StringBuffer() is vanished, automatically it could be garbage collected.

If you consider the following lines of code

Object a = new Object();
a = null;

new Object() is eligible for garbage collection at the second line itself.
0
gudii9Author Commented:
>>>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
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Mick BarryJava DeveloperCommented:
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
0
gudii9Author Commented:
>>>

   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
0
Mick BarryJava DeveloperCommented:
it doesn't
0
gudii9Author Commented:
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
0
gudii9Author Commented:
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
0
Mick BarryJava DeveloperCommented:
> At what point will the object created on line 8 be eligible for garbage collection?

its talking about the *object* created
at line 8 Manchester gets created

at line 13 there is no longer any reference to Manchester so it can be collected
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.