Running finalizers on JVM exit is disabled by default. It can be enabled with ``++System.runFinalizersOnExit++`` and ``++Runtime.runFinalizersOnExit++``, but both methods are deprecated because they are are inherently unsafe.
According to the Oracle Javadoc:
____
It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.
____
If you really want to be execute something when the virtual machine begins its shutdown sequence, you should attach a shutdown hook.
== Noncompliant Code Example
----
public static void main(String [] args) {
...
System.runFinalizersOnExit(true); // Noncompliant
...
}
protected void finalize(){
doSomething();
}
----
== Compliant Solution
----
public static void main(String [] args) {
Runtime.addShutdownHook(new Runnable() {
public void run(){
doSomething();
}
});
//...
----
== See
* https://wiki.sei.cmu.edu/confluence/x/4jZGBQ[CERT, MET12-J.] - Do not use finalizers