first of all: System.GC () is not executed immediately, but according to the various algorithms of the virtual machine to calculate the time to perform garbage collection, in addition, the program automatically end does not perform garbage collection.
Second: When the object is recycled, it goes through two tokens, the first token, if the finalize is overridden, or if Finalize is called, then garbage collection does not perform finalize, second token, if the object cannot successfully save itself in Finalize, It's really going to be recycled.
The virtual machines we use are typically sun's hotspots, and the following descriptions are for hotspot virtual machines, and of course other virtual machines,
, he is based on the search algorithm to determine which garbage needs to be recycled
gcroot is to follow, garbage collection is divided into the Cenozoic and Laosheng generation, you just new objects, occupy the memory will not exceed
directly into the standard of the Laosheng generation, since there are few objects that can survive, so the Cenozoic uses the algorithm of replication ,
If your object instance does not reach this reference chain, then it is marked as the object to be recycled, and the new generation of memory
The model is divided into Eden Space and S0 and S1, which are filtered after the object is marked for cleanup, if the object has not been called
Finalize method and rewrite the Finalize method, the object is added to a queue called F-queue, and then
The virtual opportunity initiates a lower-priority finalizer thread to execute the Finalize method of this object, if before these methods end
This object has a reference chain to the GC root. So this is the object this time to save the success of the garbage collector will not hand collect him,
of course, the Finalize method of the same object instance will only be called once, that is, there is only one chance to save.
so there are three cases of being called
1. After the object is invalidated, it is immediately recycled and the Finalize method is called.
2. In the area of E, S0, S1 or O, the heap area is 100%, and all objects that have failed in the area are recycled, triggering the Finalize method.
3. P-Zone reaches 100%,JVM interrupts all current processes, reclaims all defunct objects, triggers the Finalize method
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The Finalize method in Java is understood