How to refuse to help themselves

How to refuse to help themselves

background story

In Java, Object is all class 祖先.

img/1.png

Object class has a subclass left implemented method finalize (), its role is to be left as it is recovered in the last garbage object 自救机会.

Garbage found

JVM memory management mechanism would otherwise require the programmer to manually release the memory can be automatically released to clean up this way though a lot of convenience, but because it is automatic to identify, certainly not as precise manual release, so all kinds of garbage judgment way there.

Reference counting

Key Reference counting is to give each instance an additional citations value, when A is an invocation B, referenced B number increments by one, a reference minus a fail state, when the reference number 0, it means this example We need to be recovered.

This algorithm is simple, although saving the object count value for each additional space is needed, but it also has a fatal problem that can not solve the problem of circular references.

When the two object instances refer to each other, which will be caught in an endless loop.

Reachability analysis

Reachability analysis lot of garbage collection algorithms is the use of language.

Through from a GC Root (usually a static variable area object method referenced objects referenced in the virtual machine stack, the thread has been started but not stopped, JNI handles) object node starts, it references the object down search example, when an object can be searched, which means it is reachable, no recovery.

When an object can not be connected together with the GC Root, is the need to recover the object.

In the process of reachability analysis, if the program is still running, it is possible to cause the object to be marked as a junk re-referenced, so this process is triggered STW (Stop the world), the application will be stopped, so that the garbage collector to seize the initiative.

Stop The World

STW is realized according to the security point, the point is to ensure the security thread execution position to ensure safety before garbage collection. Select the security point of reference is usually to change the code snippet does not happen currently.

Life or death

Even when an object reachability analysis is marked as spam, but will not be eliminated immediately, but into a state of probation, wait until the next time GC, the absence of bail, perform cleanup work.

That is a mark to recover from garbage to go through at least two GC, the first is to determine the release of the object is garbage, the garbage collector will then determine whether there is replication garbage objects finalize () method, whether before or has been performed finalize () method, finalize is the last chance to save themselves the object of trash, garbage objects in line with the conditions of the finalize () will be added to a low-priority thread queue F-queue,

Although garbage to save themselves by finalize a thing sounds like a very self-improvement, but already garbage collection because the original STW let us pause occurred in the process, if there is also a garbage objects finalize some complex self-help, then a bunch of objects accumulated down on the president to the STW process can not imagine.

System class provides a runFinalizeion () method to let the JVM to finalize the implementation of active object is destroyed, but it is not controllable, maybe because it will lead to finalize junk slow down and a bunch of objects can not be collected, resulting in our program OOM.

2

This is in java.lang.ref Finalizer source code, you can see which defines a Queue, and some add and remove, are interested can go and see.

3

In runFinalizer is to call the garbage objects finalize () method of the process, you can see the catch, failed to finalize method is simply ignored the fact, that even if we want to live this object, the special exceptions in this object will die, but the dead do not understand, the virtual machine does not tell us anything.

So for the sake of performance, finalize method is best not to overwrite the object, if the object is what needs to be performed in the recovery operation, Cleaner can be used to customize our own approach, processing in a separate thread, rather than in the STW.

Guess you like

Origin www.cnblogs.com/LexMoon/p/finalizer.html