Android development experience-do not expect the class finalize method to do the job you want to do

Source: Internet
Author: User

The reason I wrote a blog about the Finalize method is because it was trapped by this method, and when I wrote a read of the JNI data class, I called the close file and freed the memory method in the Finalize method of the class, resulting in an exception being reported in the JNI when the class was called frequently. Class is a singleton, the reason for analysis should not exist in this case, to the final analysis is because in the Finalize method of the class to call the method of closing the file, resulting in the next time to open the file again, the system called the Finalize method to shut down, the code below the exception.

public class tracehandle{static{try{system.loadlibrary ("Tracehandle");        }catch (Unsatisfiedlinkerror ule) {log.e ("JNI", "Warning:could not load tracehandle.so");        }} private Tracehandle (String filePath) {mfilepath = FilePath;    Open (FilePath); }/** * Instantiate tracehandle * * */public static Tracehandle Create (String filePath) {if (null = = M        Tracehandle) {mtracehandle = new Tracehandle (FilePath);        } mtracehandle.minitcount++;    return mtracehandle;     /** * Destroy Tracehandle * * @return NULL when exiting.        */Public Tracehandle destory () {minitcount--;            if (Minitcount = = 0 && Mtracehandle! = null) {mtracehandle.close ();        Mtracehandle = null;    } return null;            } private void Celan () {if (mtracehandle! = null) {mtracehandle.close ();        Mtracehandle = null; }    }    @Override protected void Finalize () throws throwable{super.finalize ();    This is the method called by the system, the system will be called according to the system environment, for the program it calls the actual unforeseeable Celan ();    }//1, open file private native int open (String tracepath);    2, search for the specified Chinese characters private native int[] Search (byte[] wordarray);        3, must close the file private native Boolean close ();    private int minitcount = 0;    Private String Mfilepath = null; private static Tracehandle mtracehandle = null;}
     

After reviewing the information and reading the relevant chapters in Java core technology, the problem is finally settled. The Finalize method is described in the book "Java Core technology":

"You can add a finalize method to any one class. The Finalize method is called before the garbage collector clears the object. in practice, do not rely on using the Finalize method to reclaim any scarce resources, because it is difficult to know when this method will be able to be invoked.

if a resource needs to be shut down immediately after it is used, it needs to be managed manually. You can apply a drop similar to dispose or close to complete the appropriate cleanup operation. It is particularly important to note that if a class uses such a method, it must be called when the object is no longer in use. ”

The revised code follows, removing the Finalize method:

public class tracehandle{static{try{system.loadlibrary ("Tracehandle");        }catch (Unsatisfiedlinkerror ule) {log.e ("JNI", "Warning:could not load tracehandle.so");        }} private Tracehandle (String filePath) {mfilepath = FilePath;    Open (FilePath); }/** * Instantiate tracehandle * * */public static Tracehandle Create (String filePath) {if (null = = M        Tracehandle) {mtracehandle = new Tracehandle (FilePath);        } mtracehandle.minitcount++;    return mtracehandle;     /** * Destroy Tracehandle * * @return NULL when exiting.        */Public Tracehandle destory () {minitcount--;            if (Minitcount = = 0 && Mtracehandle! = null) {mtracehandle.close ();        Mtracehandle = null;    } return null;            } private void Celan () {if (mtracehandle! = null) {mtracehandle.close ();        Mtracehandle = null; }    }    1. Open file Private native int open (String tracepath);    2, search for the specified Chinese characters private native int[] Search (byte[] wordarray);        3, must close the file private native Boolean close ();    private int minitcount = 0;    Private String Mfilepath = null; private static Tracehandle mtracehandle = null;}

because Java has its own garbage collection mechanism, you must not rely on some of its methods to reclaim resources (such as FINALIZE,GC) when writing code, because the timing of these methods is unpredictable.

Resources:

1. Why does not have the use of Finalize () method in Java

2. When was the Finalize () method called in Java?


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.