Understanding Finalize () - Decorative function alternatives In many ways, Java is similar to C . Java's syntax is very similar to C , Java has class, methods and data members; Java classes have constructor; Java has an abnormal handling. However, if you use C , you will find that Java will lose some of you may be the characteristics you are familiar with. One of these features is the destructor. Substituting the destructor, Java supports the femance () method. In this article, we will describe the difference between femalize () and C destructor. In addition, we will create a simple applet to demonstrate how Finalize () works. The final limit is different from Java, and C supports local objects (stack) and global objects (based on stack). Because of this pair support, C also provides automatic constructors and sectors, which leads to calls to constructor and destructive functions, (for heap objects) is the allocation and release of memory. In Java, all objects have resident in the heap memory, so the local object does not exist. As a result, Java designers do not need a destructive function (like C ). Instead, Java defines a special method called Finalize (), which provides some features of the C destructor. However, Finalize () is not entirely the same as the destructor of C , and can assume that it will lead to a series of issues. A key element for the Finalize () method is a Java garbage collector. Garbage collectors in C / C , Pascal, and several other programming languages, developers have a responsibility to play a positive role in memory management. For example, if you assign memory for an object or data structure, you must release the memory when you no longer use it. In Java, when you create an object, the Java Virtual Machine (JVM) assigns memory for the object, calls the constructor and starts tracking the object you use. When you stop using an object (that is, when there is no reference to the object), the JVM marks the object as the release state through the garbage collector. When the garbage collector will release an object's memory, it calls the Finalize () method of the object (if the object defines this method). The garbage collector operates in an independent low priority manner, only when the other thread hangs waiting for the memory release, it starts running the memory of the object. (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 pay attention. First, Finalize () is executed only when the garbage collector releases the memory of the object. If the garbage collector does not release the memory before an 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 try to release the memory of the object that is no longer used. In other words, this is completely possible: an applet assigns memory to a small amount of object, does not cause serious memory requirements, so that the garbage collector does not release the memory of these objects. Obviously, if you define a femalize () method for an object, JVM may not call it because the garbage collector has not released the memory of those objects.
Call system.gc () does not work because it is just a suggestion for JVM instead of command. What is the advantage of finalize ()? If Finalize () is not a destructor, JVM does not necessarily call it, you may puzzle whether it is good under any circumstances. In fact, it doesn't have much advantage in Java 1.0. According to Java documents, Finalize () is a method for release non-Java resources. However, JVM has a large Finalize () method that may not call the object, so it is difficult to prove that the release of resources using this method is effective. Java 1.1 solves this problem by providing a system.runfinalizersonexit () method. (Do not confuse this method with the System.Runfinalization () method in Java 1.0.) The system.runfinalizersoneXit () method is not immediately tried to start the garbage collector immediately. Instead, when the application or applet exits, it calls the femalize () method of each object. As you may guess, forced garbage collectors to clear the memory of all independent objects by calling System.RunfinalizersoneXIT () methods, which may cause significant delays when clerifying code execution. Now establish an example applet to demonstrate how Java garbage collectors and finalize () methods are interacting. Recycling garbage begins with Java Applet Wizard to start with a new applet. When prompted to do this, type final_things as an AppleT name and choose not to generate a source file comment. Next, in the third step in Java Applet Wizard, do not select multi-thread options. The description of the Applet is modified as needed before the fifth step. When you click Finish, Applet Wizard will generate a new work space and create a default Java file for the project. Select the appropriate code input from the list A (we have highlighted the code you need to enter). When you complete the code's input, configure the Internet browser to write the output information of System.out to the javalog.txt file. (Select to build Java Logging in the Advanced page of the IE Options dialog.) Compile and run the applet. Then, wait for the Applet to run (you will see the Applet's prompt information), exit the browser and open the javalog.txt file. You will find information similar to the following row: 1000 Things Constructed 0 Things finalized As you can see, the establishment of 1,000 objects still did not force the garbage collector to start recycling space, even if there is no object to be used even when applet exits . Now, delete the comment in the first row in the STOP () method to start using the System.gc () method. Compile and run the applet again, wait for the applet to complete the run and exit the browser. When you open a javalog.txt file again, you will see the following line: 1000 Things Constructed 963 Things Finalized This time, the garbage collector believes that most objects are not used and reclaim them. In order, the JVM calls their femalize () method when the garbage collector begins to release the memory of these objects. Inherit Finalize ()? By, if you define Finalize () in the class, it will not automatically call the method in the base class.