Function
Understanding Finalize ()-destructor substitution
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.
Recycling waste
Start by using Java applet Wizard to create a new applet. When prompted to do so, enter Final_things as the Applet name and select Do not generate source file comments.
Next, take the third step in Java Applet Wizard, and do not select multithreaded options. Before step fifth, modify the description of the Applet as needed.
When you click Finish, the Applet Wizard generates a new workspace and creates a default Java file for the project. Select the appropriate code input from list A (we have highlighted the code you need to enter).
When you have finished typing the code, configure the Internet browser to write the System.out output to the Javalog.txt file. (Choose Java Logging in the Advanced page of the IE Options dialog box.) )
Compile and run the Applet. Then, wait for the applet to run (you will see the applet started in the status bar), exit the browser, and open the Javalog.txt file. You will find information similar to the following lines:
1000 things constructed
0 things finalized
As you can see, the creation of 1,000 objects still does not force the garbage collector to start reclaiming space, even when the Applet exits without the object being used.
Now, remove the annotation in the first row of the Stop () method to use the System.GC () method. Compile and run the applet again, wait for the applet to finish running, and exit the browser. When you open the Javalog.txt file again, you will see the following line:
1000 things constructed
963 things finalized
This time, the garbage collector thinks most objects are unused and reclaims them. In order, the JVM calls their finalize () method when the garbage collector starts releasing the memory of these objects.
Inherit Finalize ()?
Incidentally, if you define Finalize () in a class, it will not automatically call methods in the base class. After we discussed the differences between Finalize () and C + + destructors, this conclusion will not be surprising, since another class that is custom-defined for a class is not necessarily needed.
If you decide to call the Finalize () method in the base class by deriving the Finalize () method of a class, you can handle it like any other inheritance method.
protected void Finalize ()
{
Super.finalize ();
Other finalization code ...
}
In addition to allowing you to control whether to perform cleanup operations, this technique also allows you to control when the Finalize () method of the current class is executed.
Conclusion
What is useful, however, is that the Java automatic garbage collector does not lose balance. As a convenience, you have to give up control of the release of system resources. Unlike destructors in C + +, the Java Applet does not automatically execute the Finalize () method in your class. In fact, if you are using Java 1.0, even if you try to force it to call the Finalize () method, you cannot be sure that it will be invoked.
Therefore, you should not rely on finalize () to perform the resource cleanup work for your applets and applications. Instead, you should explicitly clear those resources or create a try...finally block (or similar mechanism) to implement.
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.