• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

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.
0
gudii9
Asked:
gudii9
  • 4
  • 4
2 Solutions
 
objectsCommented:
> 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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
objectsCommented:
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
 
objectsCommented:
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
 
objectsCommented:
> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now