Re: Doing one last thing to a WeakReference
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Wed, 18 Jun 2008 08:28:14 -0700 (PDT)
Lew wrote:
Joshua Bloch, /Effective Java/
<http://java.sun.com/docs/books/effective/index.html>,
Item 7, "Avoid finalizers".
Tom Anderson wrote:
And Lew, this is especially for you:
http://weblogs.java.net/blog/tball/archive/2005/08/finally_a_good.html
The evils of finalizers, AND error logging!
That article suggests heavy use of finalizers, which according to
everything I've read would cause slowdowns in the application.
Whether those slowdowns are significant is another question. I think
I'll stick with Mr. Bloch's advice and only use 'finalize()'
sparingly.
Lew:
They are not guaranteed to run at all.
Tom:
Isn't the only situation where they won't run at all if the program exits?
Not in principle, no. If there isn't enough pressure on memory to
force a collection after the Java object is dereferenced, then
'finalize()' will not happen.
OK, yes, because then by definition the program will exit without
running 'finalize()'.
Even if it does eventually run, it may have been delayed long enough
to put pressure on other resources. For example, if 'finalize()'
releases a resource connection, it may take so long to get around to
'finalize()' that the program runs out of available connections.
If 'finalize()' does get called in time to release the resource, it
still can cause trouble. Because it takes at least two GC cycles to
release a finalizable object, it puts extra pressure on memory.
Things are further complicated by the scenario proposed by the OP,
where additional objects are referenced from the logic. Doing that
from 'finalize()' can cause weirdness.
Lew:
When one must use finalizers there are ways to minimize the impact, for
example to use a helper class that has a non-trivial 'finalize()' that
another class composes, and to release the reference to the finalizable
member before releasing the containing instance.
Tom:
I'm not sure i [sic] quite understand. Is this for situations where there are
other objects hanging off the outer class, so you don't get them sucked
into finalization limbo?
Yes. Not the 'outer' class, but the finalizable one.
If a finalizable object holds a lot of references, their GC is
delayed, too.
This is all out there in the literature. That's where I got the
information, and maybe I don't remember it as well as I should. GIYF.
--
Lew
.
- Follow-Ups:
- Re: Doing one last thing to a WeakReference
- From: Tom Anderson
- Re: Doing one last thing to a WeakReference
- References:
- Doing one last thing to a WeakReference
- From: Paul J. Lucas
- Re: Doing one last thing to a WeakReference
- From: Tom Anderson
- Re: Doing one last thing to a WeakReference
- From: Lew
- Re: Doing one last thing to a WeakReference
- From: Tom Anderson
- Re: Doing one last thing to a WeakReference
- From: Tom Anderson
- Doing one last thing to a WeakReference
- Prev by Date: Re: Making a string, file-safe (file-encode??)
- Next by Date: httpRequest resource not available
- Previous by thread: Re: Doing one last thing to a WeakReference
- Next by thread: Re: Doing one last thing to a WeakReference
- Index(es):
Relevant Pages
|