Fate of {System|Runtime}.runFinalization (Re: Ad removing finalize eventually

Alan Bateman Alan.Bateman at oracle.com
Tue Aug 3 06:32:19 UTC 2021

On 02/08/2021 21:52, Hans Boehm wrote:
> I think we will eventually need something analogous to 
> runFinalization() for Cleaners and/or References. If you are out of 
> some resource managed by Cleaners and want to allocate another one, 
> how do you wait for resources of that kind to be reclaimed? What do 
> you do if you're testing code using Cleaners or References to reclaim 
> resources?
> This may also be a great opportunity to resolve the long-standing 
> issue of whether it's safe to call runFinalization() with user-visible 
> locks held. What happens when runFinalization's caller holds a lock 
> needed by a finalizer? This should be more tractable for Cleaners and 
> References, since it's easier to maintain separate queues for 
> different classes.

When using cleaners or references then the typical response to resource 
exhaustion will be System.gc with back-off/retry in a loop until the 
resource can be allocated. Privileged ops in the JDK can do a bit better 
by polling the pending reference list to see if there is still work to 
do. So all a bit primitive.

It's not clear to me that it's worth trying to do anything with 
runFinalization now. Yes, it is deadlock prone. It might help if it were 
changed from an untimed to a timed-wait when waiting for the secondary 
finalizer thread but that may just move the problem for finalizers that 
interact with locking.


More information about the jdk-dev mailing list