Versions Compared

Key

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

...

  • 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 . The Java Language Specification fails to define this behavior, leaving it to the individual implementations.

...

  • Wiki Markup
    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 the {{main()}} 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|AA. Bibliography#Boehm 05]\] for additional information.

...

Superclasses that use finalizers impose additional constraints on their extending classes. Consider an example from JDK 1.5 and earlier. The following noncompliant code example allocates a 16 MB buffer used to back a Swing JFrame object. Although none of the JFrame APIs have a lack finalize() method methods, JFrame extends AWT.Frame, which does have a finalize() method. When a MyFrame object becomes unreachable, the garbage collector cannot reclaim the storage for the byte buffer because code in the inherited finalize() method might refer to it. Consequently, the byte buffer must persist at least until the inherited finalize() method for class MyFrame completes its execution and cannot be reclaimed until the following garbage-collection cycle.

...

The class SubClass overrides the protected finalize() method and performs cleanup activities. Subsequently, it calls super.finalize() to make sure its superclass is also finalized. The unsuspecting BaseClass calls the doLogic() method, which happens to be overridden in the SubClass. This resurrects a reference to SubClass such that it is prevented not only prevents it from being garbage-collected but also prevents it from using calling its finalizer to close new resources that may have been allocated by the called method. As detailed in rule MET05-J. Ensure that constructors do not call overridable methods, if the subclass's finalizer has terminated key resources, invoking its methods from the superclass might lead one to observe the result in the observation of object in an inconsistent state. In some cases, this can result in NullPointerException.

...

Wiki Markup
Joshua Bloch \[[Bloch 2008|AA. Bibliography#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 the ["Initializedinitialized Flagflag" - compliant solution|OBJ11-J. Be wary of letting constructors throw exceptions#OBJ04-EX1] discussed in discussed in rule [OBJ11-J. PreventBe wary accessof toletting partiallyconstructors initializedthrow objectsexceptions#OBJ04-EX1]. As always, a good place to call the termination logic is in the {{finally}} block.

...

Code Block
bgColor#ccccff
protected void finalize() throws Throwable {
  try {
    //...
  }
  finally {
    super.finalize();
  }
}

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9f07eb783339d5da-680c95e9-42a94b8e-b2a3b2d1-54542c3fe570ff8daff94a11"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

[finalize()

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize()]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="58fb1520d5531207-0887e94c-4de34c0a-bad5a5cf-a33bd01b3e6c54c92db7a320"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 7. Avoid finalizers

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6fc0ac7d4c7e68d0-59ee068e-405d4989-bc6c97e2-16febc19bd3c842e229d7ba5"><ac:plain-text-body><![CDATA[

[[Boehm 2005

AA. Bibliography#Boehm 05]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="372bc7d81e4ef030-b5e03609-43da422b-8dc9bddc-b903bd7052ea6972ffdf85a7"><ac:plain-text-body><![CDATA[

[[Coomes 2007

AA. Bibliography#Coomes 07]]

"Sneaky" Memory Retention

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="44b07122deb1310c-45494769-4a534cf3-975a89ba-339cd41364b34775f7d68aa2"><ac:plain-text-body><![CDATA[

[[Darwin 2004

AA. Bibliography#Darwin 04]]

Section 9.5, The Finalize Method

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="685f7de88558f87f-66886f47-40cf483d-8674b85f-33d6993d4b3ce37a966ed850"><ac:plain-text-body><![CDATA[

[[Flanagan 2005

AA. Bibliography#Flanagan 05]]

Section 3.3, Destroying and Finalizing Objects

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a443ebddce93d049-1d805897-461f4025-8da798c1-bf76829a2cc5382bae141c9b"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

§12.6, Finalization of Class Instances

]]></ac:plain-text-body></ac:structured-macro>

...