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

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post