Finalizers are unpredictable, often dangerous, and generally unnecessary. Their use can cause erratic behavior, poor performance, and portability problems. Finalizers have a few valid uses, which we'll cover later in this item, but as a rule of thumb, finalizers should be avoided. C++ programmers are cautioned not to think of finalizers as the analog of C++ destructors. In C++, destructors are the normal way to reclaim the resources associated with an object, a necessary counterpart to constructors. In the Java programming language, the garbage collector reclaims the storage associated with an object when it becomes unreachable, requiring no special effort on the part of the programmer. C++ destructors are also used to reclaim other nonmemory resources. In the Java programming language, the try-finally block is generally used for this purpose. There is no guarantee that finalizers will be executed promptly. It can take arbitrarily long between the time that an object becomes unreachable and the time that its
finalizer is executed. This means that nothing time-critical should ever be done by a finalizer. For example, it is a grave error to depend on a finalizer to close open files because
open file descriptors are a limited resource. If many files are left open because the JVM is tardy in executing finalizers, a program may fail because it can no longer open files.The promptness with which finalizers are executed is primarily a function of the garbage
collection algorithm, which varies widely from JVM implementation to JVM implementation.
The behavior of a program that depends on the promptness of finalizer execution may
likewise vary. It is entirely possible that such a program will run perfectly on the JVM on
which you test it and then fail miserably on the JVM favored by your most important customer. Tardy finalization is not just a theoretical problem. Providing a finalizer for a class can, under rare conditions, arbitrarily delay reclamation of its instances. A colleague recently debugged a long-running GUI application that was mysteriously dying with an OutOfMemoryError. Analysis revealed that at the time of its death, the application had thousands of graphics objects on its finalizer queue just waiting to be finalized and reclaimed. Unfortunately, the finalizer thread was running at a lower priority than another thread in the application, so objects weren't getting finalized at the rate they became eligible for finalization. The JLS makes no guarantees as to which thread will execute finalizers, so there is no portable way to prevent this sort of problem other than to refrain from using finalizers.
Not only does the JLS provide no guarantee that finalizers will get executed promptly, it
provides no guarantee that they'll get executed at all. It is entirely possible, even likely, that a
program terminates without executing finalizers on some objects that are no longer reachable. As a consequence, you should never depend on a finalizer to update critical persistent state. For example, depending on a finalizer to release a persistent lock on a shared resource such as a database is a good way to bring your entire distributed system to a grinding halt.