...
- Do not depend on a finalizer for updating critical persistent state: It is possible for the JVM to terminate without invoking the finalizer on an unreachable object. As a result it is not advisable to use any lock or sharing based mechanisms within an initializera finalizer. Methods such as
System.gc
,System.runFinalization
,System.runFinalizersOnExit
andRuntime.runFinalizersOnExit
are either just marginally better or have been deprecated due to lack of safety and deadlock causing effects.
Wiki Markup "TheAccording Javato programmingthe languageJava imposes no ordering on finalize method calls. Finalizers may be called in any order, or even concurrently."Language Specification: \[[JLS 05|AA. Java References#JLS 05]\] Section 12.6.2: Finalizer Invocations are Not Ordered
The Java programming language imposes no ordering on finalize method calls. Finalizers may be called in any order, or even concurrently.
- 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.
...
- A possibility exists such that the programmer unintentionally resurrects the references in the
finalize
method. While the garbage collector must determine yet again whether the object is free to be deallocated, thefinalize
method is not invoked again.
Exceptions
OBJ02-EX1: Sometimes it is necessary to use finalizers especially while working with native objects/code. This is because the garbage collector cannot re-claim memory from code written in another language. Again, the native process must not perform any critical jobs that require immediate resource deallocation.
...