Understanding Finalize ()-a substitute for a destructor

Source: Internet
Author: User
Tags exception handling valid

In many ways, Java is similar to C + +. Java syntax is very similar to C++,java classes, methods, and data members; Java classes have constructors; Java has exception handling.

However, if you use C + + you will find that Java also lost some of the features that may be familiar to you. One of these features is the destructor. Instead of using destructors, Java supports the Finalize () method.

In this article, we will describe the difference between finalize () and C + + destructor. In addition, we will create a simple Applet to demonstrate how Finalize () works.

The final boundary

Unlike Java, C + + supports local objects (based on stacks) and global objects (based on heaps). Because of this dual support, C + + also provides automatic construction and destructor, which results in calls to constructors and destructors (for heap objects) is the allocation and release of memory.

In Java, all objects reside in heap memory, so local objects do not exist. As a result, Java designers do not feel the need for destructors (as implemented in C + +).

Instead, Java defines a special method called Finalize (), which provides some of the functionality of the C + + destructor. However, finalize () is not exactly the same as a C + + destructor, and can be assumed to cause a series of problems. A key element of the Finalize () method is the Java garbage collector.

Garbage collector

In C + +, Pascal, and several other programming languages, developers have a responsibility to play an active role in memory management. For example, if you allocate memory for an object or data structure, you must release that memory when you no longer use it.

In Java, when you create an object, the Java Virtual Machine (JVM) allocates memory for the object, calls the constructor, and starts tracking the objects you use. When you stop using an object (that is, when there is no valid reference to the object), the JVM marks the object as released through the garbage collector.

When the garbage collector is about to release the memory of an object, it calls the Finalize () method of the object (if the object defines this method). The garbage collector runs in a separate, low-priority way, and it begins to run the memory that frees the object only when other threads suspend waiting for that memory release to occur. (In fact, you can call the System.GC () method to force the garbage collector to release the memory of these objects.) )

In the above description, there are some important things to be aware of. First, Finalize () is executed only when the garbage collector frees the object's memory. If the garbage collector does not release memory before the Applet or application exits, the garbage collector will not call Finalize ().

Second, unless the garbage collector thinks that your Applet or application requires additional memory, it will not attempt to free up memory of objects that are no longer in use. In other words, this is entirely possible: an Applet allocates memory to a small number of objects without causing a serious memory requirement, so the garbage collector exits without releasing the memory of those objects.

Obviously, if you define a Finalize () method for an object, the JVM may not call it because the garbage collector has not freed the memory of those objects. Calling System.GC () also does not work because it simply gives the JVM a recommendation rather than a command.

What are the advantages of finalize ()?

If Finalize () is not a destructor, the JVM does not necessarily call it, and you may wonder whether it is good in any case. In fact, it doesn't have a lot of advantages in Java 1.0.

According to the Java Document, Finalize () is a method for freeing non-Java resources. However, the JVM has a very large finalize () method that may not invoke the object, so it is difficult to prove that releasing resources using this method is valid.

Java 1.1 solves this problem in part by providing a system.runfinalizersonexit () method. (Do not confuse this method with the System.runfinalizations () method in Java 1.0.) Unlike the System.GC () method, the System.runfinalizersonexit () method does not immediately attempt to start the garbage collector. Instead, when an application or Applet exits, it calls the Finalize () method of each object.

As you might guess, forcing the garbage collector to purge the memory of all stand-alone objects by calling the System.runfinalizersonexit () method can cause significant delays when the code is cleared for execution. Now set up an example Applet to demonstrate how the Java garbage collector and Finalize () methods interact.

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.