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
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)
>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.
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).]
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