About Garbage Collector and Finalize ()

xiaoxiao2021-03-06  107

This is a program that tests Finilize ():

Class Fin {string s = "begin ..."; fin () {system.out.println (this.s);} fin (string s) {system.out.println (this.s s);} public Void finalize () {system.out.println ("closing ...");}}

Public Class T4Eleven {public static void main (string args []) {

FIN A = New FIN ("Hello!") ;? new fin () ;? system.gc ();

New fin (); "FIN B = New FIN (" Hello! Again. ") ;? System.gc ();}} result: begin ... Hello! Begin ... Begin ... Begin .. .Hello! Again.closing ... Closing ...

But if the program is changed to: Class Fin {string s = "begin ..."; FIN () {system.out.println (this.s);} fin (String s) {system.ord.Println (THIS.S s);} public void finalize () {system.out.println ("closing ...");}}

Public Class T4Eleven {public static void main (string args []) {

FIN A = New FIN ("Hello!") ;? New fin () ;? new fin () ;? system.gc ();

FIN B = New FIN ("Hello! Again.") ;? System.gc ();}} The result will become: begin ... Hello! Begin ... begin ... Closing ... Begin ... Hello! Again.Closing ... =============

Finalize is called when JVM garbage collection. Basically garbage collection is unacceptable, as long as JVM discovers an object without any reference, it will reclaim it. System.gc () just explaining the code recommended JVM for garbage collection.

================

/ * Well, garbage collection is this, its principle is a compiler technology called "reference count" technology, that is, each created object, in a certain place in the virtual machine, saving objects The number of referenced, if not references to an object, then that object is garbage.

However, JVM will not recover the garbage immediately, and when JVM discovery is insufficient, it will only recover all garbage, which is actually better, frequent recycling garbage will reduce the efficiency of the system. Of course, you can also display garbage recycling to recycle garbage (System.gc ()).

The execution result of the first program is also wrong, it should be: begin ... Hello! Begin ... Closing ... Begin ... begin ... Hello! Again.Closing ... For better understanding of garbage Recycling, here is attached to a clear example: the main body is still used in the original code, but add a private integer data ID as an identity of the object, and then defines a static integer variable count to count the objects generated by the Fin. number. The constructor is only unfolded, the following is the complete code: * /

//t4eleven.javaclass fin {int id = 0; // Record the identification number static int count = 0; // Count the number of objects fin () {count ; setId (count); system.out.println (" FIN object " ID " constructed ");}

Private Void SetID (INT ID) // Sets the identification number of the object {THIS.ID = ID;}? public void finalize () {system.out.println ("FIN Object" ID "Recycled");}}

Public Class T4Eleven {public static void main (String args []) {FIN A = New Fin (); // Object 1 is constructed, and will reference new fin (); // Object 2 is constructed, but an anonymous object It can only be referenced once. It is used as garbage system.gc (); // Object 2 is recycled, and the object 1 is used because there is a reference, does not look for garbage, do not recover new fin (); // object 3 Construction, I didn't point to its reference, which is the garbage new fin (); // object 4 is constructed, (同) FIN B = New Fin (); // Object 5 is constructed SYSTEM.GC () /// The objects 3 and 4 are recycled, and the object 5 is breaded because there is bread, do not look for garbage, not recycling ??}}

/ * Execution: --------- Fin object 1 constructed FIN object 2 is constructed Fin class object 2 is recycled FIN object 3 constructed Fin object 4 constructed Fin object 5 is constructed The FIN object 3 is recycled FIN object 4 is reclaimed --------- * // * When new fin () generates an object, it is an anonymous object (one of the compiler will give it one Name) and fin a = new fin (); what is done, first constructing an object (actually understanding is anonymous, then returning its address to a, also said A references that anonymous object), so Strictly speaking, A is actually an object. It is just a reference to an object. In Java, in order to facilitate communication and understanding, consensus, it is a consensus to see a Fin object, but everyone must be clear, it is actually It is a reference to an object. * / ===============

"Is it that the situation would have different differences in the mechanism of JVM, and the order of the output results is different?"

"Is there any GARBAGE COLLECTION in SYSTEM.GC ()?"

Not forced, just suggesting that JVM is garbage collection, JVM has complete power to ignore this suggestion

"In fact, I use JDK1.2.1"

It is estimated that the problem is different from the version.

?

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

New Post(0)