Time to retire System.runFinalizersOnExit?
peter.levart at gmail.com
Tue Jan 27 12:54:47 UTC 2015
On 01/27/2015 10:49 AM, Aleksey Shipilev wrote:
> On 27.01.2015 07:37, Mandy Chung wrote:
>> System.runFinalizationOnExit has been deprecated since 1998 (JDK 1.2)
>> and this method is inherently unsafe. I am thinking to propose this method
>> in JDK 9 to throw UnsupportedOperationException.
>> I believe it's rare for existing applications using
>> My analysis on Maven Central ~315K artifacts that show about ~15 unique
>> artifacts calling System.runFinalizationOnExit while they all come from
>> only 5 classes.
> I think while the use case for runFinalizationOnExit(true) is weird to
> begin with, there seems to be a valid reason to enforce the finalizers
> to run at the end, e.g. to properly free the *critical* underlying
> resources. At this point rFOE(true) trick is, while unsafe, still
> operational, right? If we are to remove rFOE(true) escape hatch, what do
> we propose users to do?
I think that finalizers as designed can not be used to properly shutdown
the program running in a VM as a whole, since they are inherently
limited to private iter-independent resources like primitive system
resources (file handles, memory handles, etc...). And those don't need
explicit "proper" shutdown as OS usually takes care of them when the
process ends. Admittedly this does not happen if VM is used embedded in
a process that does not end when the VM exits.
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).
To support orderly shutdown of a program running in a VM, other non-JDK
solutions are available (app containers like Spring, etc...).
More information about the core-libs-dev