Java: finalize method and garbage collection

The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls its object’s finalize() method before performing garbage collection.

If you want to perform garbage collection directly in the code description process, you can use the finalize() method when creating a class to add the process of returning resources such as files or sockets used in the object.

protected void finalize() throws Throwable {
  items = null;
  super.finalize();
}
finalize() Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post