What happens if you save a reference to the current object during the finalize call? For example:
class foo { ... public void finalize() { bar.REFERENCE = this; } }
Is the object garbage-collected, or not? What happens when you try to access bar.REFERENCE
later?
finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
“This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.” So, in one way we can not guarantee the execution and in another way we the system in danger.
Finalize method in Java is an Object Class method that is used to perform cleanup activity before destroying any object. It is called by Garbage collector before destroying the object from memory.
The finalize method will be called after the GC detects that the object is no longer reachable, and before it actually reclaims the memory used by the object. If an object never becomes unreachable, finalize() will never be called on it. If the GC doesn't run then finalize() may never be called.
The object is not garbage collected. This is know as "Object resurrection".
You must be careful with that, once the finalizer is called the gc won't call it again, on some enviroments like .NET you can re-register the finalizer but i'm not sure about java
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