...
Wiki Markup According to the Java Language Specification \[[JLS 2005|AA. Java References#JLSBibliography#JLS 05]\] Section 12.6.2 "Finalizer Invocations are Not Ordered"
This can be a problem as slow running finalizers tend to block others in the queue.Wiki Markup The Java programming language imposes no ordering on {{finalize}} method calls. Finalizers \[of different objects\] may be called in any order, or even concurrently.
...
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 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 (not from the {{main()}} thread). If a finalizer is necessary, the cleanup data structure should be protected from concurrent access. (See \[[Boehm 2005|AA. JavaBibliography#Boehm References#Boehm 05]\].)
Noncompliant Code Example
...
Wiki Markup |
---|
According to the Java API \[[API 2006|AA. Java References#APIBibliography#API 06]\] class {{System}}, {{runFinalizersOnExit()}} method documentation |
...
Wiki Markup |
---|
Joshua Bloch \[[Bloch 2008|AA. Java References#BlochBibliography#Bloch 08]\] suggests implementing a {{stop()}} method explicitly such that it leaves the class in an unusable state beyond its lifetime. A {{private}} field within the class can signal whether the class is unusable. All the class methods must check this field prior to operating on the class. This is akin to *OBJ04-EX1* discussed in guideline [OBJ04-J. Do not allow partially initialized objects to be accessed]. As always, a good place to call the termination logic is in the {{finally}} block. |
...
Wiki Markup |
---|
Alternatively, a more expensive solution is to declare an anonymous class so that the {{finalize()}} method is guaranteed to run for the superclass. This solution is applicable to {{public}} non-final classes. "The finalizer guardian object forces {{super.finalize}} to be called if a subclass overrides {{finalize()}} and does not explicitly call {{super.finalize}}". \[[JLS 2005|AA. JavaBibliography#JLS References#JLS 05]\] |
Code Block |
---|
public class Foo { // The finalizeGuardian object finalizes the outer Foo object private final Object finalizerGuardian = new Object() { protected void finalize() throws Throwable { // Finalize outer Foo object } }; //... } |
...
Wiki Markup |
---|
\[[JLS 2005|AA. Java References#JLSBibliography#JLS 05]\] Section 12.6, Finalization of Class Instances \[[API 2006|AA. Java References#APIBibliography#API 06]\] [finalize()|http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize()] \[[Bloch 2008|AA. JavaBibliography#Bloch References#Bloch 08]\] Item 7, Avoid finalizers \[[Darwin 2004|AA. Java References#DarwinBibliography#Darwin 04]\] Section 9.5, The Finalize Method \[[Flanagan 2005|AA. Java References#FlanaganBibliography#Flanagan 05]\] Section 3.3, Destroying and Finalizing Objects \[[Coomes 2007|AA. JavaBibliography#Coomes References#Coomes 07]\] "Sneaky" Memory Retention \[[Boehm 2005|AA. JavaBibliography#Boehm References#Boehm 05]\] \[[MITRE 2009|AA. Java References#MITREBibliography#MITRE 09]\] [CWE ID 586|http://cwe.mitre.org/data/definitions/586.html] "Explicit Call to Finalize()", [CWE ID 583|http://cwe.mitre.org/data/definitions/583.html] "finalize() Method Declared Public", [CWE ID 568|http://cwe.mitre.org/data/definitions/568.html] "finalize() Method Without super.finalize()" |
...