A finalizer does not need to set an object's fields to null to help the garbage collector. At the point in the Java object life-cycle when the finalize method is called, the object is no longer reachable from the garbage collection roots. Explicitly setting the object's fields to null does not cause the referenced objects to be collected by the garbage collector any earlier, and may even adversely affect performance.

The life-cycle of a Java object has 7 stages:

The call to the finalize method occurs when the object is in the 'Collected' stage. At that point, it is already unreachable from the garbage collection roots so any of its references to other objects no longer contribute to their reference counts.

Ensure that the finalizer does not contain any null assignments because they are unlikely to help garbage collection.

If a finalizer does nothing but nullify an object's fields, it is best to completely remove the finalizer. Objects with finalizers severely affect performance, and you should avoid defining finalize where possible.

In the following example, finalize unnecessarily assigns the object's fields to null.

  • J. Bloch, Effective Java (second edition), Item 7. Addison-Wesley, 2008.
  • IBM developerWorks: Explicit nulling.
  • Oracle Technology Network: How to Handle Java Finalization's Memory-Retention Issues .
  • S. Wilson and J. Kesselman, Java Platform Performance: Strategies and Tactics, 1st ed., Appendix A. Prentice Hall, 2001.