Versions Compared

Key

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

...

Because of these problems, finalizers must not be used in new classes.

Noncompliant Code Example (Superclass' finalizer

...

)

...

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 the JFrame APIs lack finalize() 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.

Code Block
bgColor#ffcccc
class MyFrame extends JFrame {
  private byte[] buffer = new byte[16 * 1024 * 1024];
  // persists for at least two GC cycles
}

Compliant Solution (Superclass' finalizer

...

)

When a superclass defines a finalize() method, make sure to decouple the objects that can be immediately garbage collected from those that must depend on the finalizer. This compliant solution ensures that the buffer can be reclaimed as soon as the object becomes unreachable.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fa5f7168ab21bf22-6a20fa94-4d3a4d93-9a2cb0e2-40485e228bf8d6e1a24b6887"><ac:plain-text-body><![CDATA[

[[API 2006

AA. 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="f966f3ae3107b6fa-e420f5c6-498642ea-822cbb55-74f91726a9fcb987ea2da557"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. 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="5765b53191a9e571-35bd6504-405f4f54-9a899a31-9f0aaabe76d558a48b787029"><ac:plain-text-body><![CDATA[

[[Boehm 2005

AA. 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="a0b142e84e879f25-56e001c8-42f14697-b680bb98-f93a2f86e764584728505fc1"><ac:plain-text-body><![CDATA[

[[Coomes 2007

AA. 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="a4cf919d76b76761-4ba1fa9a-4b734552-8de6b323-31050d34932cc19f794d587d"><ac:plain-text-body><![CDATA[

[[Darwin 2004

AA. 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="ebdedc9d788d2150-9b1ad2f5-4ef34e40-94638fa8-918a4b154ea5f2a843ba2a1e"><ac:plain-text-body><![CDATA[

[[Flanagan 2005

AA. 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="5f7501caa13572bc-39ccc412-40ec4fb0-9082983c-f265b3a9e983c4a6dbcf7d4e"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. References#JLS 05]]

§12.6, Finalization of Class Instances

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

...