Possible Duplicate:
method finalize and exceptions
The garbage collector invokes finalize()
method when an object is about to released from memory. What happens when an exception is raised in the finalize()
method?
Will GC proceed further and release the memory, or GC will halt the process for that object?
If the exception is raised in a try-catch statement and is correctly caught then yes, "the GC will proceed further", as it does in regular programs.
But if the thrown exception is not handled then, according to the JSL,
If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates.
So, in this case the "GC will halt the process for that object" and in which case it may be that some its resources are not have been correctly released.
By the way, the finalizer has 2 drawbacks :
You cannot be sure that the finalize method will be ever be executed (from this blog)
Why do the above if the garbage collector will always call finalize before freeing up memory associated with an object? The reason is because the garbage collector doesn't always get a chance to clean up objects before the JVM terminates.
Finalizers force objects to be promoted to old space (that takes longer to collect), and postpone the GC until the finalizer is run, adding more overhead to the GC algotithm. (cf Java performance Tuning 2nd,Edition by Jack Shirazi).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With