Before it reclaims storage from an object that is no longer referenced, the garbage collector calls finalize()
on the object.
This is a good time to release resources held by the object.
Because the general contract is that the finalize
method should only be called once per object, calling this method explicitly is
misleading and does not respect this contract.
What is the potential impact?
An explicit call to an object’s finalize method will perform operations that most likely were supposed to be performed only when the object was not
referenced anymore by any thread.
Since it is an acceptable practice to override the finalize method in any subclass of Object
, by invoking it explicitly, we will run
code that was designed to only be ran at a different time.
Noncompliant code example
public void dispose() throws Throwable {
this.finalize(); // Noncompliant
}