Why is this an issue?

Before it reclaims storage from an object that is no longer referenced, the garbage collector calls finalize() on the object.

But there is no guarantee that this method will be called as soon as the last references to the object are removed.

It can be few microseconds to few minutes later.

For this reason relying on overriding the finalize() method to release resources or to update the state of the program is highly discouraged.

What is the potential impact?

More unexpected issues can be caused by relying on the finalize() method to perform important operations on the application state:

Noncompliant code example

public class MyClass {

  @Override
  protected void finalize() {
    releaseSomeResources();    // Noncompliant
  }

}

Resources