Finalize () method in Object class:

1. Source code: Protected void Finalize () throws Throwable{} 2, Finalize () method has only one method body, there is no code in it The JVM garbage collector is responsible for calling the finalize() method: When an object is about to be collected by garbage collector, garbage collector calls Finalize () method 5. If you want to execute a code at the time of object destruction, the code will be written to Finalize () method 6. Garbage collector will not start easily, because garbage is too little and time is not up, it may start under various conditions. It may not startCopy the code

Call the Finalize () and System.gc() methods

public class Test06{ public static void main(String[] args){ /* Person p = new Person(); // the Person object becomes garbage p = null; /* for(int I = 0; i < 1000000; i++){ Person p = new Person(); p = null; } */ for(int i = 0; i < 100000; i++){ Person p = new Person(); p = null; If (I % 2 == 0){// If (I % 2 == 0){// If (I % 2 == 0){// If (I % 2 == 0){ }}}} class Person{// Rewrite the Finalize () method // call p.finalize() when objects of type Person are collected by garbage collector; Protected void finalize()throws Throwable{system.out.println (this + "about to be destroyed "); }}Copy the code