DanT's GridBlog  
All | Chess | Cooking | Grid | Java | Misc | Travel
 
Thursday September 07, 2006
To Finalize Or Not To Finalize

Because of some API work I'm currently doing, I was recently faced with the question of whether or not to use a finalizer to simplify the API. Searching on the Internet turns up lots of articles (mostly from IBM) saying that finalizers are bad and should be avoided. Since I work for Sun, I thought it would be a good idea to find out what our official stance is. This post is a summary of the answers I got.

First off, a little background reading. These articles were recommended to me by Sun engineering:

In addition, I found the following articles helpful:

From those articles and from direct feedback from various Sun engineers, these are the lessons about finalizers that I have gathered:

  1. If there's a way around using a finalizer, and it's not ridiculously more complicated, do it. See Tony Printezis' article for some alternatives to finalizers. In many cases, an explicit dispose() method is the best choice.
  2. Never trust a finalizer to free something that has no other way of being freed. There is no guarantee that a finalizer will ever run. For example, deleting lock files is something that definitely should not happen in a finalizer, unless it also happens somewhere else.
  3. If you're designing an API that may have a JNI-based implementation, either design in explicit dispose() methods, or make sure there's a common clean-up point where held resources can be freed, such as exiting the session, so that implementers are not forced to use finalizers.
  4. The Java language's specification of finalizers has some holes which allow you to do bad things (such as restoring a reference to the object being finalized). If forced to use a finalizer, do so carefully.

In my end, my findings were exactly what my initial searches suggested. It is, however, nice to have confirmation.

Permalink Comments [1] (2006-09-07 10:14:02.0/2006-09-07 10:14:02.0)
Trackback: http://blogs.sun.com/templedf/entry/to_finalize_or_not_to
 
Trackback URL: http://blogs.sun.com/templedf/entry/to_finalize_or_not_to
Comments:

There is also good advice in "Effective Java" item 6: Avoid finalizers You've listed some great resources. Unfortunately, this information isn't in the Javadoc for Object. Finalizers and Shutdown hooks are two related areas where Java could be considerably improved. "Enhancements for the Shutdown Hooks API" http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5036297

Posted by Curt Cox on September 07, 2006 at 11:53 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed
 
Calendar
« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
Blog::Navigation
Bookmarks::Grid Engine
Bookmarks::Blogroll
Bookmarks::News
Link to DanT's GridBlog

Link to DanTs GridBlog

Site notes

This page validates as XHTML 1.0, and will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device. It was created using techniques detailed at glish.com/css/.
Powered by Roller Weblogger.

XML