• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Rahul Ramachandran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
I have a doubt regarding garbage colletion and the finalize() method.
It is known that the finalize() method is invoked on an object when it is garbage collected, and that the finalize() method is run only once on an object.
If the finalize() method is overriden and the object is instead resurrected, then the next time the object needs to be garbage collected, the finalize() method WILL NOT be invoked on it. Correct? That means, it will be GONE (I am not using the term garbage collected), when it next is left simply dangling without a reference.
Well, my real doubt now follows: In JQ+, I found a few questions like: "The finalize() method is ALWAYS called before an object is garbage collected, true or false". Answer is invariably TRUE.
My answer is FALSE.
What say you ranchers?
Thanks a lot.
Rahul
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with your explanation.
But,
Invoking finalize method before GCing on objects can be seen like this

it will be definitely called on non-resurrected objects before GCing
&
on resurrected objects it was already called once.
On that basis can we say that finalize() method will be definitely called on object before GCing..
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should have said
"The finalize() method is ALWAYS called before an object is garbage collected (for the first time), true or false".
Maybe Paul Anil will change the wording for us.
Hey Paul!!!
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3752
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tvs sundaram already pointed it out.
An object cannot be garbage collected twice. Indeed, once it is destoyed, it is destroyed. How can you destroy it again. Calling finalize and destroying an object are 2 diff. things. So, there is no such thing as "for the first time" because there is only one time!
So the point is, if the object is actually being destroyed (ie. being garbage collected), finalize() method has already been called.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Try out the world's only WebCompiler!
www.jdiscuss.com
[This message has been edited by Paul Anil (edited May 15, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wellllllll, I just re-read it - and it IS technically true. I guess it is just that we tend to think of finalize happening IMMEDIATELY before garbage collecting, and while that is USUALLY true, it is a mistake to think that it is ALWAYS true.
So - Paul is right.
 
mousami bhattacharya
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that mean that for all objects the finalize method is ALWAYS called (I mean its guarenteed to be called ) before it goes for garbage collection.
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
joseph smyth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the finalize will get called before being processed by the GC but this is not to say the finalize method will definitely get called.
This is half question half statement. Am I correct with this?
Joe
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The finalize will ALWAYS get called exactly ONE time before the object is garbage collected (ie. destroyed - notice the past tense).
That does NOT mean that the finalize will get called each time the garbage collector examines the object.
***************
1 object has no references
2 garbage collector examines object
3 garbage collector calls finalize on object
4 finalize resurrects object
5 object again has no references
6 garbage collector again examines object
7 finalize has ALREADY happened so garbage collector immeditately destroys object WITHOUT a second call to finalize.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic