aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic
Author

GC

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Helo!
What happens here:

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
(SCJP pending)
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Antti,
Not 100% sure what you're trying to do. Setting an object to null won't make it eligible for gc; you have to set all the references to the object to 'null'.
Is that what your after; a way to find all references and set them to null??
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What means:
you have to set all the references to the object to 'null'.
Could you apply this to the example code I wrote? The idea is to make object eligble for gc created by getSimple lazy initialisation pattern.

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
(SCJP pending)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Does code above guarantee that there is no leaks expected on field obj?
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
(SCJP pending)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
On JRE 1.4 (Windows) following code

generates this output

Is this predictable behaviour?
So we do not have to pay any attention for leaks?
Why the output of following modification

is this?

Am I understanding finalizer method at all?
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
(SCJP pending)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
>Does code above guarantee that there is no leaks expected on field >obj?
Neither of the obj = null is useful.
If the finalize method is called the CombineSimple object is unrechable, so it has no sense setting field to null
The answer to your last question is very simple. References are not saved in any variable.

SCJP2. Please Indent your code using UBB Code
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Hi Antti,
As an answer to the code

and questionDoes code above guarantee that there is no leaks expected on field obj?
No bcoz it should be noted that when you are calling getSimple you are copying the reference and sending it to the one that called it.Here,the referent (Simple object in heap) has two references pointing it.And simply putting null to only the private member does no good.
I want Jose to, please clear the statement
If the finalize method is called the CombineSimple object is unrechable..

And for the code second code:
When System.gc() is called in main() it invokes the finalize() in combineSimple class.This is the way that Gc works before GC'ing any object.But since it's your version of finalize() it does call yours.It is not doing cleanup at all since you override it to print messages only.To ensure cleanup is performed you should call super.finalize() and define any special task that GC can't take care of,file,Connections,etc.After all this finalize, there is no guarantee that GC will be performed.
And the last code:
Since the reference is not saved in any reference variable,it is eligible for GC as soon as the new one is created in the loop.
Please correct if wrong.
THANKS.

[This message has been edited by Bindesh Vijayan (edited September 01, 2001).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
First a question Bindesh how do you cite other's responses? I do not know to do it
>If the finalize method is called the CombineSimple object is >unrechable..
The g.c. calls a finalize if the object is not strong, soft or weakly reachable. So we can say that a finalize method is only called when there are not references to the object apart from a phantom one.
>It is not doing cleanup at all since you override it to print >messages only.
What else could be done in this example?
>To ensure cleanup is performed you should call
>super.finalize() and define any special task that GC can't take >care of,file,Connections,etc.After all this finalize,
super.finalize() it is only needed if the parent class defined finalize() otherwise the finalize method defined in Object does nothing.
Bruce Eckel tell us that these kind of operations are risky to execute whithin a finalize precisely because the uncertenty of the execution of the finalize method for a particular object.
So it's better doind them in a separate method called when you have done with the object.
Read more about this on
artima.com , a site by Bill Venners who kindly put on line some chapters of his book Inside The Java 2 Virtual Machine
Or downlodad the excellent book Thinking in Java by Bruce Eckel from bruceeckel.com
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Helo Jose!
First a question Bindesh how do you cite other's responses? I do not know to do it

See http://www.javaranch.com/ubb/ubbcode.html

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
 
subject: GC