The use of the "Tij Learning note" Java Finalize ()

Source: Internet
Author: User

Java programming thought mentions that Java's garbage collector is not so reliable, garbage collection consumes a lot of resource overhead, garbage collector is lazy, when the variables and objects are no longer referenced, out-of-scope, the garbage collector will be garbage collection, that is, when the memory resources are tense, The garbage collector will proactively reclaim resources to free up memory at the fastest rate possible, and may not be recycled at other times.

If Finalize () is called automatically when the garbage collector is working, looking at the source code of the object class will find that this method is an empty method (regardless of the underlying), is that the way we can rewrite it?

 PackageCom.github.hellxz.thinkinjava; Public classTestfinalize { Public Static voidMain (string[] args) {/** Create Finalizemethod class and Invoke*/testfinalize F=Newtestfinalize ();        F.finalize (); System.out.println ("Mian Method End"); }        /** Methods for overriding the object class*/    protected voidFinalize () {System.out.println (The Finalize method executes the); }}
Output:

The Finalize method executes the
Mian Method End

Looking at the above code, we confirm that this method of the object class can be rewritten, in the book that Finalize is the method that is called before the garbage collector deletes the object, then modify the above code to test

 PackageCom.github.hellxz.thinkinjava; Public classTestfinalize { Public Static voidMain (string[] args) {/** Create Finalizemethod class and Invoke*/testfinalize F1=Newtestfinalize (); Testfinalize F2=Newtestfinalize (); Testfinalize f3=Newtestfinalize (); Testfinalize f4=Newtestfinalize (); System.out.println ("Mian Method End"); }        /** Methods for overriding the object class*/    protected voidFinalize () {System.out.println (The Finalize method executes the); }}//Output://Mian Method End

Although we have created so many objects and are not used, the rewritten finalize method is not executed, which means that the garbage collector is not recycled. Java has System.GC () to force the garbage collector to end the action; to make the code more convincing, modify the code structure to make it more intuitive

 PackageCom.github.hellxz.thinkinjava; Public classTestfinalize { Public Static voidMain (string[] args) {Apple A1=NewApple (1); Apple A2=NewApple (2); Apple A3=NewApple (3); Apple A4=NewApple (4); A1=NULL;//empty A1A2 =NULL;//empty A2System.GC (); System.out.println ("Mian Method End"); }}classapple{Private inti;  PublicApple (inti) {         This. i =i; System.out.println ("Apple" + i + "is created!"); }        protected voidFinalize ()throwsThrowable {Super. Finalize ();//calling the object class methodSystem.out.println ("Apple" + i + "is disposed!"); }}//Output://Apple 1 is created!//Apple 2 is created!//Apple 3 is created!//Apple 4 is created!//Mian Method End//Apple 1 is disposed!//Apple 2 is disposed!

Conclusion:

1, do not rely too much on the Finalize method to perform resource cleanup work, the garbage collector will automatically reclaim resources

2, finalize different from the C + + destructor, do not need to actively free memory, want to invoke can use System.GC ();

3. Finalize is used to clear non-Java language occupied memory, and check for errors

4. The garbage collector will not reclaim memory if it has garbage, unless memory is tight

The use of the "Tij Learning note" Java Finalize ()

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.