Enabling runFinalizersOnExit
is unsafe as it might result in erratic behavior and deadlocks on application exit.
Indeed, finalizers might be force-called on live objects while other threads are concurrently manipulating them.
Instead, if you want to execute something when the virtual machine begins its shutdown sequence, you should attach a shutdown hook.
Noncompliant code example
fun main() {
System.runFinalizersOnExit(true) // Noncompliant
}
Compliant solution
fun main() {
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run() {
doSomething()
}
})
}