Java Language finalize() method


This is a protected and non-static method of the Object class. This method is used to perform some final operations or clean up operations on an object before it gets removed from the memory.

According to the doc, this method gets called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

But there are no guarantees that finalize() method would gets called if the object is still reachable or no Garbage Collectors run when the object become eligible. That's why it's better not rely on this method.

In Java core libraries some usage examples could be found, for instance in

protected void finalize() throws IOException {
    if ((fd != null) &&  (fd != {
        /* if fd is shared, the references in FileDescriptor
         * will ensure that finalizer is only called when
         * safe to do so. All references using the fd have
         * become unreachable. We can call close()

In this case it's the last chance to close the resource if that resource has not been closed before.

Generally it's considered bad practice to use finalize() method in applications of any kind and should be avoided.

Finalizers are not meant for freeing resources (e.g., closing files). The garbage collector gets called when (if!) the system runs low on heap space. You can't rely on it to be called when the system is running low on file handles or, for any other reason.

The intended use-case for finalizers is for an object that is about to be reclaimed to notify some other object about its impending doom. A better mechanism now exists for that purpose---the java.lang.ref.WeakReference<T> class. If you think you need write a finalize() method, then you should look into whether you can solve the same problem using WeakReference instead. If that won't solve your problem, then you may need to re-think your design on a deeper level.

For further reading here is an Item about finalize() method from "Effective Java" book by Joshua Bloch.