| Author |
Garbage Collection
|
ramesh maredu
Ranch Hand
Joined: Mar 15, 2008
Posts: 210
|
|
Hi All, I have one query in garbage collection concept, in K&B book below statement is given. "If first time an object�s finalize() method runs and the object is saved from the Garbage Collection, then the second time that object is about to be Garbage Collected, finalize() will not run". My query is, if we call finalize method in our code, and assume that same object is about to be garbage collected, finalize() will be called again by garbage collector?. [ October 02, 2008: Message edited by: ramesh maredu ]
|
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
|
 |
chander shivdasani
Ranch Hand
Joined: Oct 09, 2007
Posts: 206
|
|
No, Finalize will be called only once. Suppose in Finalize method, you reassign a reference to the same object, so that object will be saved from Garbage collection. And the next time the same Object is garbage collected, the Object's finalize wont run. [ October 03, 2008: Message edited by: chander shivdasani ]
|
Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
|
 |
M Srilatha
Ranch Hand
Joined: Aug 27, 2008
Posts: 137
|
|
My query is, if we call finalize method in our code, and assume that same object is about to be garbage collected, finalize() will be called again by garbage collector?. If i get your question correctly, we have a method say m1 in a class which calls finalize() method. That method is called once and the object is available for GC. Does Garbage collector calls finalize() method or not? According to me, it will call finalize() method. As the stmt which is given below is about garbage collector calling finalize() method. "If first time an object�s finalize() method runs and the object is saved from the Garbage Collection, then the second time that object is about to be Garbage Collected, finalize() will not run". But i am not sure.Correct me If i am wrong!
|
Thanks,<br />Srilatha M
|
 |
raj malhotra
Ranch Hand
Joined: Feb 22, 2007
Posts: 285
|
|
|
it doesn't matter if you call finalize() method yourself in the code.It will be as good as any other method in an object. But when GC calls finalize method on an object it will never call it again on the same object.
|
 |
ramesh maredu
Ranch Hand
Joined: Mar 15, 2008
Posts: 210
|
|
Hi Srilatha, you got me correct. Thanks all, so if we call finalize() method on our object it will not be called again by the garbage collector.
|
 |
M Srilatha
Ranch Hand
Joined: Aug 27, 2008
Posts: 137
|
|
so if we call finalize() method on our object it will not be called again by the garbage collector
No..this is not correct! If finalize() method is called by garbage collector once, it wont be called by garbage collector again. Even if we call finalize() method directly from our code, garbage collector may/may not call finalize() method before GC takes place. But we cannot say that finalize() method wont be called again by garbage collector. I hope this will clear your doubt!
|
 |
ramesh maredu
Ranch Hand
Joined: Mar 15, 2008
Posts: 210
|
|
If finalize() method is called by garbage collector once, it wont be called by garbage collector again. Even if we call finalize() method directly from our code, garbage collector may/may not call finalize() method before GC takes place
Thank you, this explanation answers my question well.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
the rule is that the garbage collector will always run finalize on the objects it garbage collects. also it will call it once on every object not more than that. It has nothing to do with how many times you have called finalize on an object. The only case that finalize will not run on a object is when the JVM exits before the object is garbage collected...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
 |
|
|
subject: Garbage Collection
|
|
|