Finalize method use case
package test; class TestGC { private String str = "hello"; TestGC(String str) { this.str = str; } public void finalize() { System.out.println(str) ; } } public class Hello { /* * * @param args */ public static void main(String[] args) { // TODO automatic generation method stub System.out.println("hello"); TestGC test = new TestGC("test1"); test = new : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : TestGC("test2"); test = null;//Comment out this sentence and test1 is recycled. Add test2 first, then test1 System.gc(); } }
The finalize() method is defined in the Object class, so all classes inherit it. The subclass overrides the finalize() method to organize system resources or perform other cleaning work. The finalize() method is called on the object before the garbage collector deletes it.
The above is an introduction to the usage of Java garbage collection finalize(). I hope it will be helpful to everyone's learning.