wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes garbage collector example 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 example" Watch "garbage collector example" New topic
Author

garbage collector example

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You need to read the code completely, and go through it with a pencil and paper, marking which object is pointed to by which reference variable, and rubbing out references as they change.
Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
I was trying on a paper with pencil.

sb, sb2 refer to Mancheser stringbuffer object where as sb3 point to Chester stringbuffer objects. when sb2 points to Mancheser stringbuffer object then sb disconnected from Mancheser stringbuffer object thus when you say sb2=null sb is eligible for garbage collection. please advise
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Mathew Lee wrote: . . . when you say sb2=null sb is eligible for garbage collection. . . .
Correct

That is, I believe line 13, which was the answer given on the website.
Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
>>>when sb2 points to Mancheser stringbuffer object then sb disconnected from Mancheser stringbuffer object

why does sb disconnected from Mancheser stringbuffer object. i was not clear on that. please advise
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Mathew Lee wrote:

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.


Not exactly.

A developer might call finalize() on an object. If she does (which she shouldn't because it's a bad habit), finalize() will still run if the object gets garbage collected. She might call finalize() a hundred times and the object will still be fine, alive and well to continue to do object things.

Also,
There is no (none, zero, zip) guarantee that finalize() will ever be called on an object. The only guarantee is that finalize() will be called zero or at most one time by the JVM. This is because the GC may never run, or may run and not GC that object.

And furthermore,
It's worth saying here that if, for some reason, the JVM's call to finalize() on an object happens to resurrect it somehow -- finalize() will never be called on that object again by the JVM... even if it's eligible for GC.


When you do things right, people won't be sure you've done anything at all.
Sze Kong Chan
Greenhorn

Joined: Mar 30, 2011
Posts: 10
Mathew Lee wrote:>>>when sb2 points to Mancheser stringbuffer object then sb disconnected from Mancheser stringbuffer object

why does sb disconnected from Mancheser stringbuffer object. i was not clear on that. please advise


So basically, 2 objects are created Manchester StringBuffer and Chester Stringbuffer.
At the end of line 10, sb and sb2 reference Manchester, while sb3 reference Chester.
After the execution of (line 11) sb=sb3, => sb2 reference Manchester, while sb and sb3 reference Chester. Note Manchester doesn't qualify for GC because sb2 is still referencing the object
Only after the execution of (line 13) sb2 = null, => Manchester is not reference, hence it is qualify for GC.

I am looking at the problem from the perspective of when the object is not reference, I feel that it will be easier.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Janeice, are you suggesting those questions are inaccurate?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Campbell Ritchie wrote:Janeice, are you suggesting those questions are inaccurate?


I was suggesting there is a technical nuance / semantic here:

Finalize will always be run before an object is garbage collected


Saying the word "always" with the phrase "before an object is GC" is dangerous.

The finalize method MAY NOT be run at the time when an object is destroyed. It may have been called by the JVM already and will never be called by the JVM again. So, technically, before an object is GC is correct (because things have to happen to an object before it's destroyed, as OP noted), but that's (in my opinion) not necessarily the spirit of the question because some folks get hung up thinking that it HAS to happen when the object is GC -- right before. The danger is in relying on the finalize() method to close resources or do anything important, because there is no guarantee it will run at the time of Object death, or at all.

And adding clarification here:

there is no guarantee when finalize will run, only that it will run before garbage collection happens.


... because the finalize() method may never be called at all. The object may never GET destroyed, even if it's eligible. Another technical semantic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Thank you, Janeice
Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
>>>Only after the execution of (line 13) sb2 = null, => Manchester is not reference, hence it is qualify for GC.

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



Sze Kong Chan
Greenhorn

Joined: Mar 30, 2011
Posts: 10
Mathew Lee wrote:
sb2=null,
does it null sb2 object reference on stack or Manchester String Buffer object on heap.

it set the value of the sb2 reference to null, Manchester StringBuffer is still on the heap.

Mathew Lee wrote:
when sb2=null does that means sb=null automatically?

no.




Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
then as per the question how objet created in line 8 ie

>>sb = new StringBuffer("Manchester");

is eligible for garbage collection. please advise
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
I though you had already worked that out: there are no longer any references pointing to it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collector example