aspose file tools
The moose likes Java in General and the fly likes finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "finalize method" Watch "finalize method" New topic
Author

finalize method

Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
Hi

I have one question:

What will happen if i have garbage collection(call to system.gc()) in finalize method?


Gunjan Kumar
SCJP 1.5
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

when the instance is garbage collected, it may result in a call to collect the garbage, which may result in garbage being collected. Or maybe not, it's all a bit fluid with GC. If you're implying it may result in an infinite loop I strongly doubt it. As mentioned, System.gc() is just a request to clean the garbage, it does not enforce it.
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
Ya

I had a doubt about infinite loop.

But my question is : whether that object will be garbage collected or not?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2300
Either you have called the GC (JVM might not even call the GC when you say System.gc()) , or the JVM has called the GC , that is the reason that you have reached the finalize method of the objects to be collected.

Now if you again call GC from the finalize method , the GC might not be even called , as this has been called from within the finalize method.


Rahul Bhattacharjee
LinkedIn - Blog
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It definitely won't be garbage-collected while finalize() is still running, if that's what you're worried about.

Calling System.gc() is almost always misconceived. Having a finalize() method is usually misconceived too. So calling System.gc() in finalize() is at the extreme end of bad ideas. But you won't get a crash or a hang (this isn't C++!). You just won't get whatever effect you were trying to achieve.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Purvesh Vora
Ranch Hand

Joined: Dec 10, 2003
Posts: 47
I have question on the similar lines.

What if I call this.someMethod() in the finalize method? Will the object get garbage collcted?
If not then again finalize method will be called? I think its called only once in the lifetime of object. So next time the object would be garbace collected automatically?

Thanks & Regards,
Purvesh
 
jQuery in Action, 2nd edition
 
subject: finalize method
 
Similar Threads
about finalize() method
GC problem, MindQ #37
finalize method
Finalize():will garbage collector call finalize() after my code has already called it?
Finalization Method