Time to retire System.runFinalizersOnExit?

Peter Levart peter.levart at gmail.com
Tue Jan 27 22:00:36 UTC 2015

On 01/27/2015 09:55 PM, Martin Buchholz wrote:
> If you want to ensure something is run before VM exit, add a shutdown 
> hook.
> If you really want to run all the finalizers, you can always run a 
> variant of GcFinalization in your shutdown hook, although as always 
> running during shutdown increases the risk greatly.

What do you mean by "GcFinalization" ?

When shutdown hooks run, there can be objects with finalize methods that 
are still reachable from GC roots, so they are not pending finalization 
yet. Invoking System.gc() and System.runFinalization() would not run 
finalizers for them. runFinalizersOnExit on the other hand, run the 
finalizers for all objects with finalize methods regardles of their 
"pending" status. This is dangerous of course. Even more than normal 
finalization. But there's no public API for that after 
runFinalizersOnExit is removed.

Practically, I think this only potentially affects environments where VM 
is embedded in a process that keeps running when a particular instance 
of VM already exits.


> On Tue, Jan 27, 2015 at 6:48 AM, Peter Levart <peter.levart at gmail.com 
> <mailto:peter.levart at gmail.com>> wrote:
>     On 01/27/2015 01:54 PM, Peter Levart wrote:
>         A poor-man's escape hatch is a shutdown hook that calls
>         System.runFinalization(). Which might interfere with other
>         shutdown hooks that run concurrently (runFinalizersOnExit runs
>         finalizers after all shutdown hooks are finished).
>     Not really. This only runs finalizers for Objects pending
>     finalization, while runFinalizersOnExit runs it for all Objects
>     with finalize() methods that have not been invoked yet. A big
>     difference. So there's no alternative if this method is removed.
>     Peter

More information about the core-libs-dev mailing list