Reputation: 53
I red from the following link:garbage collection from geeksforgeeks and here it is said that:
The finalize() method is never invoked more than once for any given object.
So, here it is said "more than once" and i'm wondering whether there is possibility that finalize() method is not invoked and garbage collector destroys that object.
Is it possible?
Upvotes: 2
Views: 1437
Reputation: 533492
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.
Upvotes: 2