Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] usage finalize() as last resort of resource management

Danail Nachev wrote:
The discussion which I try to find is answer to the question: why finalize() isn't used as last resort for freeing system resources as it is used for FileInputStream for example? Am I missing something here?

The last comment on the bug refers to the specific issue in this case, which is explained in the article. Some things that appear to be garbage on the Java side (because there are no Java references to them) will still be in use (because there are OS-level references to them). This bad sequence could happen:

1. Java objects initialize some resources.
2. OS sets up references to resources.
3. Java objects go out of scope.
4. Java objects are finalized.
5. OS resources are disposed.

6. Java call delegates to OS.
7. OS tries to use resources.
8. Pain and suffering.

Take the example (from the article) of an X-Windows GC and Font. A GC can refer to a Font via OS-level structures. From the Java side, the Font appears to be garbage, but it's actually still in use. If the Font is disposed by a finalizer, things will blow up in an ugly way. Your JVM dies with a Segmentation Fault or somesuch platform-specific nonsense.

--
Peter Centgraf


Back to the top