Java programming Ideas-fifth chapter-some exercises

Source: Internet
Author: User
Tags garbage collection

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.