Do finalizers (and ReferenceQueue's) run on JVM termination? [duplicate]
Asked Answered
A

1

8

I know you can't count on finalizers to clean up your mess (i.e. free resources), but I was wondering - do java objects get GC'ed / finalized when the JVM normally terminates (System.exit() / no threads left)?

EDIT:

So, the GC is not guaranteed to be triggered, hence nor finalize(), but will ReferenceQueue work?

Abrupt answered 6/8, 2013 at 6:18 Comment(10)
that's a document about CLR, not JVM you link toChampionship
Well, there is System.runFinalizersOnExit(), however it is deprecated. Anyway, when you exit a process, it is the OS which cleans up ultimately. Now, why this question?Operation
maybe you need to add some shutdown trigger using Runtime.addShutdownHook()! there is no warranty about calling finalize, and if system did, there is no warranty to let the finalize does everything it wants.Saito
@DenisTulskiy: It's a conceptual article. fge: It's a conceptual question.. But also - I wan't to free allocated resources (namely delete files). user: A shutdownHook might be a good solution.Abrupt
"... do java objects get GC'ed / finalized when the JVM normally terminates (System.exit() / no threads left)?" - No. "... but will ReferenceQueue work?" No.Thompson
"A shutdownHook might be a good solution." Not for doing finalization. It won't work.Thompson
Any Reference (pun Un intended)?Abrupt
Those were comments not Answers. Feel free to disbelieve me :-)Thompson
Don't take it personally :-), but I think I'll put it through a test (just figured I can do that pretty easily).Abrupt
The problem with testing it is that it might work, or not sometimes but with a minor change e.g. an update, a different OS, or CPU and you might find it doesn't work the same way. As it is not guaranteed, you cannot assume it will work, or not work, on all future versions on all platforms you might need.Morville
W
6

For a while, there was a method Runtime.runFinalizersOnExit which could be called with argument true to guarantee that all objects were finalized before the VM shuts down. That method is now deprecated, for reasons that are documented in the java documentation at http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html all the way at the bottom of the page. This is the relevant section:

Why is Runtime.runFinalizersOnExit deprecated?

Because it is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock. While this problem could be prevented if the class whose objects are being finalized were coded to "defend against" this call, most programmers do not defend against it. They assume that an object is dead at the time that its finalizer is called.

Further, the call is not "thread-safe" in the sense that it sets a VM-global flag. This forces every class with a finalizer to defend against the finalization of live objects!

Now that this method has been deprecated, there is no way whatsoever to guarantee object finalization. Whether and when to garbage collect an object is completely up to the JVM.

Walloping answered 6/8, 2013 at 6:29 Comment(1)
Do you happen to have any reference about References too (will they get enqueued at shutdown)? It's just that I can't see how enqueue a Reference might cause any of the issues brought up in the above article.Abrupt

© 2022 - 2024 — McMap. All rights reserved.