Method finalize in Java, exceptions

What happens to the garbage collector if the finalize() method takes a significant amount of time to execute, or if an exception is thrown during execution?

Finalize() is called directly in a separate Finalizer thread (java.lang.ref.Finalizer.FinalizerThread), which is created when the virtual machine starts (in the static section when loading the Finalizer class). Finalize() methods are called sequentially in the order in which they were added to the list by the garbage collector. Accordingly, if some finalize() hangs, it hangs the Finalizer thread, but not the garbage collector. This means, in particular, that objects that do not have a finalize() method will be properly deleted, but those that do not have a finalize() method will be added to the queue until the Finalizer thread is free, the application ends, or memory runs out.

The same applies to exceptions thrown during finalize(): the runFinalizer() method of the Finalizer thread ignores all exceptions thrown at the time of finalize() execution. Thus, the occurrence of an exception will not affect the performance of the garbage collector in any way.


Read also:


Comments

Popular posts from this blog

ArrayList and LinkedList in Java, memory usage and speed

XML, well-formed XML and valid XML

Methods for reading XML in Java