Finalize () is an Object protected method that can be overridden by subclasses to clean up resources. GC calls finalize() before collecting objects.

Finalize the role of

(1)finalize() is not corresponding to the destructor in C++. The timing of destructor calls in C++ is deterministic (objects go out of scope or delete), but the call of finalize in Java is deterministic

(2) It is not recommended to use finalize method to clean “non-memory resources”, but recommended to use finalize method to clean local objects (objects created by JNI); ② As a supplement to ensure the release of some non-memory resources (such as sockets, files, etc.), we can explicitly call other resource release methods in Finalize method. The reason can be seen in the following [Finalize] question

Finalize the problem of

(1) some methods related to finalize, due to some fatal flaws, has been abandoned, such as System. RunFinalizersOnExit () method, the Runtime runFinalizersOnExit () method

(2)System.gc() and system.runfinalization () methods increase the chance of finalize method execution, but they should not be blindly relied on

(3) The Java language specification does not guarantee that Finalize methods will be executed in time, and it does not guarantee that they will be executed at all

(4) Finalize method may bring performance problems. Because the JVM usually completes finalize execution ina separate low-priority thread

(5) Object regeneration problem: In Finalize method, objects to be reclaimed can be assigned to object references reachable by GC Roots, so as to achieve object regeneration

(6) The Finalize method can be executed by GC at most once (of course, users can call the Finalize method of objects manually, but it does not affect GC’s behavior of Finalize)

Implementation process of Finalize (Life cycle)

(1) First, describe the Finalize process roughly: When the object becomes unreachable (GC Roots), GC will judge whether the object is covered by Finalize method. If not, it will be directly recycled. Otherwise, if objects have not executed finalize method, put them into F-Queue, and let a low-priority thread execute Finalize method of objects in the Queue. After finalize method is executed, GC will judge whether the object is reachable again. If not, the object will be recycled. Otherwise, the object will be resurrected.

(2) Specific Finalize process:

An object can be composed of two states, involving two types of state space. One is the terminal state space F = {undevoted, finalizable, devoted}; The second is the reachable status space R = {reachable, finalizer-reachable, unreachable}. The meanings of each state are as follows:

Undevoted: The finalized object will enter this state first. GC is not ready to execute its Finalize method because the object is reachable

Finalizable: Indicates that GC can execute finalize method on the object and GC has detected that the object is unreachable. As mentioned above, GC completes finalize execution through f-Queue and a dedicated thread

Finalized: indicates that GC has executed the Finalize method on this object

Reachable: Indicates that GC Roots references are reachable

Finalizer-reachable (F-reachable) : indicates that the achable object is not reachable, but is reachable through a Finalizable object

Unreachable: An object is unreachable through the preceding two methods



(1) The newly created object is in the Reachable, Undevoted state. (A)

(2) With the running of the program, some reference relationships will disappear, resulting in the state transition from the reachable state to the F-reachable (B, C, D) or unreachable(E, F) state

(3) If the JVM detects that an object in the Unfinalized state becomes F-reachable or unreachable, the JVM marks it as finalizable (G,H). If the object was in the unreachable, Undevoted state, it is also marked as F-reachable (H).

(4) At some point, the JVM takes a Finalizable object, marks it as finalized, and executes its Finalize method ina thread. Because it was referenced in the active thread, the object will transition to the Reachable, Finalized state (K or J). This action will influence some other objects to return from the F-Reachable state to the Reachable state (L, M, N).

(5) An object in the finalizable state cannot be unreahable at the same time. According to the fourth point, when the finalizable object is marked as a Finalized object, a thread will execute the Finalize method of the object, making it reachable. This is why there are only eight state points in the diagram

(6) The programmer’s manual call of Finalize method will not affect the above internal tag changes, so the JVM will only call Finalize method once at most, even if the object is “revived”. How many times a programmer makes manual calls does not affect the behavior of the JVM

(7) If the JVM detects that an object in the Devoted state becomes unreachable, reclaim its memory (I).

(8) If objects do not overwrite finalize method, JVM will optimize and directly recycle objects (O)

(9) note: System. RunFinalizersOnExit () method can make the object even in a reachable state, the JVM is still on its execution method to finalize