Reference 4684465110&11
Finalize () The condition that is called
Java1.6 the following conditions:
(1) class not called (NULL) (2) Call System.GC ()
1.8 of conditions:
(1) Call System.GC (). (After a call to System.GC (), Finalize () is executed, that is, Finalize () is executed when the last '} ' is executed)
//: Main.java/** * Garbage Collection * Note: Java environment 1.6 can, 1.8 No, garbage collection mechanism changes.*/ClassTest {@OverrideProtectedvoidFinalize () {System.Out.println ("Finalize"); super.finalize (); }}class main { public static void main (string[] args) {Test t = new Test (); t = null; // Make sure Finalize () is called System.GC ();}} /* * * output:finalize *///: ~
12
When the object is cleaned, the Finalize () function is called, and the stored data, such as T2, is preserved;
When the object is cleaned, it will be stacked in the stack, cleaned first, and then cleaned up before entering.
//: Main.java/** * Garbage Collection * Note: Java environment 1.6 can, 1.8 No, garbage collection mechanism changes.*/ClassTank {Boolean isfull =False; String name; Tank (String name) {THIS.name =Name }voidSetempty () {isfull =False; }voidSetfull () {isfull =True; } @OverrideProtectedvoidFinalize () {if (!Isfull) {System.OUT.PRINTLN (name +": Clean"); }//Super.finalize ();}}ClassMain {PublicStaticvoid main (string[] args) {Tank T1 = new Tank (" t1); Tank t2 = new Tank ( "t2 "); Tank t3 = new Tank ( "t3 "); T1.setfull (); T2.setempty (); T1 = nullnull; T3 = null; System.GC (); }}
/** * output:t3: Cleanup T2: Clean *///:~ /span>
Java Programming Ideas-fifth chapter-some exercises