Thursday, 29 August 2013

Is Object memory freed when we explicitly call finalize() on it?

Is Object memory freed when we explicitly call finalize() on it?

As far as my understanding goes finalize() and GC are two different
aspects. GC uses finalize() method to free Object memory. We cannot state
when GC will occur(even if we explicitly call System.gc()). But we can
explicitly call finalize() on an Object.
Will the function be executed immediately(memory freed) or it waits till GC
occurs like System.gc() call?
Also as per the docs the finalize method is never invoked more than once
by a Java virtual machine for any given object.
So what happens when we call finalize() first and GC happens at a later
point of time.
If object memory is not freed on explicit call to object.finalize() then
would't
it being called again in the GC process violate the calling only once rule?

No comments:

Post a Comment