When the GC has found an object with class where finalize()
has been overridden it is added to a queue of objects to have finalize() called on them. It is only after the object has been finalized once, that the GC can clean it up. i.e. this would be on a later GC.
e.g. If an object is in tenured space, it might be found until a full collection is performed, and it will only be cleaned up on a full GC after the finalize method has been called.
For further details, this is the Java 11 Javadoc for Object.finalize()
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#finalize()
so is there any possibility that garbage collector fully destroyed the object in heap but didn't call the finalize() method on that object?
While the object is in the finalization queue, it can't be removed.
And you all assumed that there is not certainity in the destroying of object by the garbage collector.
It won't be destroyed while there is still a strong reference to it.
finalize()
method gets executed. The word “definitely” is inappropriate here. All the garbage collector does, is enqueuing objects needing finalization. One or more finalizer threads may process them, but the JVM might terminate before they get to a particular object. Further, a JVM is not required to support finalization at all. – Holger