Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Before the garbage collector acts on an object to reclaim it, the object's finalizer is executed. This is required to ensure that resources such as open streams, files and network connections get are released as resource management does not happen automatically while reclaiming memory. In Java, the finalize() method of java.lang.Object is used to perform for this activitypurpose.

The There are a number of caveats associated with the use of finalizers are discussed here:

  • There is no fixed time for finalizers to get executed; this detail is JVM dependent: The only thing that is guaranteed is that if at all a finalizer gets executed, it will be before the garbage collector's second cycle. An object may become unreachable and yet its finalizer may not execute for an arbitrarily long time. Nothing of No time-critical nature functionality should be run implemented in the finalize method. For instance, closing file handles is not recommended.

...

  • Effect of uncaught exceptions: An uncaught exception thrown during finalization is ignored. The finalization process itself stops immediately so it fails to accomplish its purpose.
  • Unintentional mistakes like Coding errors that result in memory leaks can also cause finalizers to never execute to completion.
  • A possibility exists such that the programmer may unintentionally resurrects resurrect the object's reference in the finalize method. While the garbage collector must determine yet again whether the object is free to be deallocated, the finalize method is not invoked again.

...

...

  • Wiki Markup
    It is not advisable to use any lock or sharing based mechanisms within a finalizer because of the inherent dangers of deadlock and starvation. On the other hand, it is also easy to miss that there can be synchronization issues with the use of finalizers even if the source program is single-threaded. This is because the {{finalize()}} methods are called from their own threads. If at all a finalizer is necessary, the cleanup data structure should be protected from concurrent access (See \[[Boehm 05|AA. Java References#Boehm 05]\]).

...

The System.runFinalizersOnExit() method is used in this noncompliant example to simulate a garbage collection run (note that this method is deprecated because of thread-safety issues MET36-J. Do not use deprecated methods).

Wiki Markup
According to the Java API \[[API 06|AA. Java References#API 06]\] class {{System}}, {{runFinalizersOnExit()}} method documentation:

...

OBJ02-EX1: Sometimes it is necessary to use finalizers especially when working with native code. This is because the garbage collector cannot re-claim memory used by code written in another language. Also, the lifetime of the object is often unknown. Again, the native process must not perform any critical jobs that require immediate resource deallocation.

In such cases, finalize should may be used correctly. Any subclass that overrides finalize() must explicitly invoke the method for its superclass as well. There is no automatic chaining with finalize. The correct way to handle this is shown below.

...