Never rely on finalize

You cannot rely on finalize to always reclaim resources. Instead, if an object has resources which need to be recovered, it must define a method for that very purpose - for example a close method, or a dispose method. The caller is then required to explicitly call that method when finished with the object, to ensure any related resources are recovered.

With JDK 7+, the try-with-resources feature can be used to reclaim resources for any object that implements AutoCloseable.

For an extensive discussion, please see the "Avoid Finalizers" topic in Effective Java.

The System.runFinalizersOnExit(boolean) method is deprecated.

See Also :
Recovering resources
Would you use this technique?
Yes   No   Undecided   
© 2018 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -