This is the mail archive of the
java-patches@sourceware.cygnus.com
mailing list for the Java project.
Re: Patch: finalize interned strings
Tom Tromey wrote:
>
> >> I'm now wondering: Does the collector guarentee that finalizers
> >> will be run immediatly when an object is collected? Or can the
> >> running of finializers be deferred until a later collection in some
> >> cases?
>
> Jeff> Finalizers should run *before* an object is collected.
>
> Yeah. I interpreted Bryce's question as "will the finalizer be run
> immediately when the object is unreachable?". And the answer is "not
> necessarily".
I may be missing something here (and I need a vacation myself).
As long as the finalizer is invoked (and completes) before the string is
collected, there is no chance of any dangling references to the string
object, and no chance of memory corruption, right? It might take a long
time for finalization to occur (it may never occur) but the consequence
is only memory retention, not corruption.
I was considering race conditions. Imagine this: an interned string
becomes collectable so it is scheduled for finalization. But before
finalization can occur, another thread calls intern() and grabs a
reference to the finalizing string object. When finalization does occur
it becomes uninterned but is not collected because it is no longer a
dead object. And the thread that called intern() thinks it has an
interned string, but doesn't.
FWIW it took Sun a long time to get this right... versions of the JDK
prior to 1.2 never released interned strings.
--
Jeff Sturm
jsturm@sigma6.com