Java Destroy Object __java

Source: Internet
Author: User
Tags finally block
The finalization method (finalizer) is generally unpredictable and dangerous, and is generally not necessary. Using the finalization method can result in erratic behavior, reduced performance, and portability issues.
Doing this in Java is largely dependent on try-finallymechanism to assist with the completion, however, Java also provides another mechanism called finalizer, where the user only needs to overload the Finalize method provided by object objects, so that when the JVM is garbage collected, the method can be invoked automatically. However, it is not recommended that users rely on this method to achieve the goal of releasing critical resources because of the uncertainty of when the object is garbage collected and the performance impact of finalizer on the GC. For example, there are thousands of graphics handles waiting to be terminated and recycled, unfortunately, the thread priority for the execution of the finalization method is lower than the average worker thread, so that a large number of graphics handle resources remain in the finalizer queue without being released in time, resulting in a decrease in the efficiency of the system, It can even cause the JVM to report outofmemoryerror errors.

The Java language specification does not guarantee that the method will be implemented in a timely manner, or even that it will be executed. Even if developers manually invoke the System.GC and System.runfinalization methods in code, this only increases the chance that finalizer is executed. It is also important to note that if an exception is thrown in the overloaded Finalize () method, its stack frame trajectory is not printed. The recommended resource release method in Java is to provide an explicit, well named interface method, such as Fileinputstream.close () and Graphic2d.dispose (). The consumer then calls the method in the finally block, see the following code:

public void Test () {
    fileinputstream fin = null;
    try {
        fin = new FileInputStream (filename);
        Do something.
    } finally {
        fin.close ();
    }
}
In the actual development, the use of finalizer can bring us what kind of help. See the following example:

public class Finalizetest {
    //@Override
    protected void Finalize () throws Throwable {
        try {
            // This method is used during debugging to print the various states of the object before it is collected, and/or to determine if there is
            still a resource not being released or if there is a state inconsistency.
            //recommends that the Finalize method be designed to be available only in the debug state, and that
            the method does not exist in release//, to avoid its effect on run-time efficiency.
            System.out.println ("The current status:" + _mystatus);
        } finally {
            //in finally the call to the superclass Finalize method is necessary to ensure that the finalize chain in the entire class inheritance
            //system is executed.
            super.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.