Versions Compared

Key

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

...

  • Wiki Markup
    According to the _Java Language Specification_, [§12.6.2, "Finalizer Invocations are Not Ordered"|http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.6.2] \[[JLS 2005|AA. Bibliography#JLSReferences#JLS 05]\]:

    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.

    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.

...

  • 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#BoehmReferences#Boehm 05]\] for additional information.

...

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

...

Wiki Markup
Joshua Bloch \[[Bloch 2008|AA. Bibliography#BlochReferences#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 "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.

...

Wiki Markup
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|AA. Bibliography#JLSReferences#JLS 05]\].

Code Block
bgColor#ccccff
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
    }
  };
  //...
}

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a59b7b4cb38d2f4f-89138d3a-41184cdb-b59bab35-ac00d215770b73e215ba6dda"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API References#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="f7f2675d330253a0-1096c19c-406944db-bfc3bb95-d943f25eb88e7a023c53ed39"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch References#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="a0602664c7ed20cc-36c36b2e-468b4d46-a705bf5b-18c99fabb6515d2b65584e5b"><ac:plain-text-body><![CDATA[

[[Boehm 2005

AA. Bibliography#Boehm References#Boehm 05]]

 

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1a24e3949f95b2bc-099535ef-43af4896-8ec0b9a1-405c79d6bf59324d5056aec8"><ac:plain-text-body><![CDATA[

[[Coomes 2007

AA. Bibliography#Coomes References#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="021329335e18f1fd-3d5b350b-4787429f-ab7592bc-afc858a40a75f87002a47e1b"><ac:plain-text-body><![CDATA[

[[Darwin 2004

AA. Bibliography#Darwin References#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="2b0c9fc09eee1af3-bb282adb-48bb4b1c-84c3bd52-401b97205b1ece3ce32b36ba"><ac:plain-text-body><![CDATA[

[[Flanagan 2005

AA. Bibliography#Flanagan References#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="a0a7d88468dcf558-6f3800e4-4e3e4798-a9298c4f-67c9b70727ea38332c73c00e"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS References#JLS 05]]

§12.6, Finalization of Class Instances

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

...