runFinalization (was: RE: JavaMemoryModel: A problematical case f or finalizers)

From: Boehm, Hans (hans_boehm@hp.com)
Date: Tue May 20 2003 - 15:28:22 EDT


[This is a bit off topic, and I'm not proposing that we address this as part of JSR 133, but ...]

The problem is that the spec for runFinalization says:

"When control returns from the method call, the Java Virtual Machine has made a best effort to complete all outstanding finalizations."

And that statement is roughly what you need if you really want to manage file descriptors completely with finalizers, and you really only have 20 of them. (Managing file descriptors completely with finalizers is not recommended, and if you only have 20 of them, I recommend switching to a different OS.)

If it does this by either directly invoking finalizers, or by waiting for another thread to do so, you get into the same deadlock possibilities. You could run each finalizer in its own thread, and wait for a while, but that may be expensive and feels like a hack. My impression is that most VMs run finalizers in the current thread, but I'm not sure about that. Many VMs also seem to invoke runFinalization from file open methods if they run out of descriptors.

My current preference would be to make it clear which routines may invoke System.runFinalization, and to state that those may not be called with user-visible locks held. This may apply to some user-defined resource allocation routines. I haven't yet found any examples for which it causes serious problems, but I clearly haven't looked hard enough either.

Hans

> -----Original Message-----
> From: Joshua Bloch [mailto:joshua.bloch@sun.com]
> Sent: Tuesday, May 20, 2003 12:04 PM
> To: Boehm, Hans
> Cc: 'Martin Trotter'; javaMemoryModel@cs.umd.edu
> Subject: Re: JavaMemoryModel: A problematical case for finalizers
>
>
> Hans,
>
> No worries. I didn't think that you were referring to my book:) I
> agree that there's lots of very bad advice floating around out there
> concerning finalizers. But I simply wouldn't go out of my
> way to make
> it any easier to write corect finalizers. Very few people
> should have
> to write them, and those who do should understand the issues.
>
> >5) There's no clear convention about the context in which
> finalizers
> can be
> >explicitly invoked. If System.runFinalization() runs finalizers in
> the calling
> >thread,
> >
> It can't! One of the few hard guarantees is that finalizers are run
> from a thread that holds no user-visible locks. (I say this from
> memory, but I'm quite sure it's in the spec.)
>
> Regards,
>
> Josh
>
>
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:44 EDT