Understand finalize () - destructor alternative

xiaoxiao2021-03-06  19

Understand finalize () - destructor alternative

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.

Final boundary

Unlike Java, C supports local objects (stack) and global objects (based on stacks). 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 collector

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 to. 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.

recycle trash

Create a new applet by using Java Applet Wizard. 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 from 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 line:

1000 Things Constructed

0 Things Finalized

As you can see, the establishment of 1,000 objects still does 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 ()?

Incident, if you define Finalize () in the class, it will not automatically call the method in the base class. After we discussed the different points of Finalize () and C , it is not surprised by this conclusion because the other class is not necessarily required for a certain class.

If you decide to invoke a Finalize () method in the base class by deriving a class's Finalize () method, you can work like other inheritance methods.

protected void finalize ()

{

Super.Finalize ();

// Other finalization code ...

}

In addition to allowing you to control whether or not to perform clear operations, this technology also allows you to control the Finalize () method of the current class.

in conclusion

However, it is beneficial that Java's automatic garbage collector does not lose balance. As a convenient price, you have to give up control of system resource release. Not like the destructor in C , Java Applet does not automatically perform the femalize () method in your class. In fact, if you are using Java 1.0, even if you try to force it to call the femalize () method, you can't make sure it will call it.

Therefore, you should not rely on Finalize () to perform the resource clearance of your applet and application. Instead, you should clearly remove those resources or create a try ... finally block (or similar mechanism).

List A: Final_things.java

Import java.applet. *;

Import java.awt. *;

Class Thing

{

Public static int exclycount = 0;

Public static int exciteFinal = 0;

Public Thing ()

{

damunt;

}

protected void finalize ()

{

dam

}

}

Public class firm_things extends applet

{

Public final_things ()

{

}

Public string getAppletInfo ()

{

Return "name: final_thing / r / n"

"Author: Tim Gooch / R / N"

"Created with Microsoft"

"Visual J Version 1.1";

}

Public void init ()

{

Resize (320, 240);

}

Public void destroy ()

{

}

Public void Paint (Graphics G)

{

g.drawstring ("CREATED with Microsoft"

"Visual J Version 1.1", 10, 20);

}

Public void start ()

{

While (Thing.THINGFinal <1)

{

NEW shing ();

}

}

Public void stop ()

{

// system.gc ();

System.out.println (Thing.THINGCOUNT

"Things constructed"); system.out.println (Thing.THINGFinal

"Things firmized");

}

}

转载请注明原文地址:https://www.9cbs.com/read-44320.html

New Post(0)