Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The caveats associated with the usage of finalizers are discussed belowhere:

  • There is no fixed time for finalizers to get executed, which again is JVM dependent: The only thing that is guaranteed is that a finalizer will be executed before the garbage collector's run. An object may become unreachable and yet its finalizer may not execute for an arbitrarily long time. Nothing of time-critical nature should be run in the finalize method, for instance, closing file handles is not recommended.

...

In such cases, finalize should be used correctly. Any subclass that overrides finalize must explicitly invoke the method for its superclass as well. There is no automatic chaining with finalize. The correct way to handle this is shown belownext.

Code Block
protected void finalize() throws Throwable {
  try {
    //...
  }
  finally {
    super.finalize();
  }
}

...