1. Overview
In the runtime data area, the program counter, virtual machine stack, and local method stack are all created and destroyed with the thread. ’s
Therefore, their memory allocation and recycling is deterministic, and they are recycled when the method or thread ends. The Java heap and method area are uncertain. The size of the objects created during the running of the program is uncertain. The size of the required memory can only be known when the program is running.
2. "Survival Algorithm"
To determine whether an object is alive, there are two main algorithms: reference counting Method and reachability analysis algorithm
Reference counting methodReference counting method is to add a reference to the object Counter, every time the object is referenced once, the counter value is incremented by 1, and the reference expiry is decremented by 1. If the counter is 0, it means it will not be used again.
The reachability analysis algorithm is a mainstream implementation , this algorithm uses the node that becomes GC ROOT as the root node,Searches downward from the root node, if there is no reference chain connecting the object to GC ROOT (that is, it is unreachable), it means that
The objects that can be used as GC ROOT are:
At present, the reachability analysis algorithm has become the mainstream algorithm. The reason is that the reference counter algorithm cannot solve the problem of
reachability Analysis and Judgment Process
After conducting the reachability analysis on the object, it is found that it does not have any reference chain connected to it, so it is marked for the first time. And perform a screening,
The filtering condition is whether the finalize() method needs to be executed. When the object does not cover the finalize method or the finalize method has been called,will automatically create a low-priority finalizer thread to execute the finalize method of these objects, and then GC will The object
in F-QUEUE will be marked for the second time. If the object cannot escape at this time, it will be recycled.finalize() method
The finalize method is mentioned many times above, please pay attention to it , the object's finalize method will only be automatically called once by the system.It is not recommended that you use this method. Its function can be completed with try-finally instead
The above is the detailed content of JVM Advanced Features-3. Garbage Collection Determining Object Survival Algorithm. For more information, please follow other related articles on the PHP Chinese website!