...
- According to the Java Language Specification, §12.6.2, "Finalizer Invocations are Not Ordered" [JLS 2005]:
One consequence is that slow-running finalizers can delay execution of other finalizers in the queue. Further, the lack of guaranteed ordering can lead to substantial difficulty in maintaining desired program invariants.The Java programming language imposes no ordering on
finalize()
method calls. Finalizers [of different objects] may be called in any order, or even concurrently.
- Uncaught exceptions thrown during finalization are ignored. When an exception thrown in a finalizer propagates beyond the
finalize()
method, the process itself immediately stops and consequently fails to accomplish its sole purpose. This termination of the finalization process may or may not prevent all subsequent finalization from executing. The Java Language Specification fails to define this behavior, leaving it to the individual implementations.
...
- Use of finalizers can introduce synchronization issues even when the remainder of the program is single-threaded. The
finalize()
methods are invoked by the garbage collector from one or more threads of its choice; these threads are typically distinct from themain()
thread, although this property is not guaranteed. When a finalizer is necessary, any required cleanup data structures must be protected from concurrent access. See the JavaOne presentation by Hans J. Boehm [Boehm 2005] for additional information.
...
According to the Java API [API 2006] class System
, runFinalizersOnExit()
method documentation,
...
Compliant Solution
Joshua Bloch [Bloch 2008] 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 the "initialized flag" – compliant solution discussed in rule OBJ11-J. Be wary of letting constructors throw exceptions. As always, a good place to call the termination logic is in the finally
block.
...
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 nonfinal 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].
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 } }; //... } |
...
CWE-586. Explicit call to | |
| CWE-583. |
| CWE-568. |
Bibliography
[API 2006] | |
Item 7. Avoid finalizers | |
| |
"Sneaky" Memory Retention | |
Section 9.5, The Finalize Method | |
Section 3.3, Destroying and Finalizing Objects | |
[JLS 2005] | §12.6, Finalization of Class Instances |
...