JVM Garbage Collection Mechanism (2)--When to Recycle

1.2 When to recycle

The previous chapter made it clear that an object "may" be garbage collected if it is not referenced.

So, how do we know that the object has no references? Of course, there is an algorithm involved here.

We introduce two commonly used algorithm implementations: Reference Counting Algorithm and Reachability Analysis Algorithm

1.2.1 Reference Counting Algorithm

①For the reference counting algorithm, we can also guess according to the literal meaning. The so-called reference counting algorithm is to establish a counter associated with the object reference. When it points to void, the counter value is -1. When the counter value is 0, it means that the object can be recycled.

It is estimated that Zhou Zhiming's in-depth understanding of the Java virtual machine is the most popular estimate, which only mentions a saying that everyone is talking about during the interview: "Add a reference counter to the object...".

In fact, this statement is a bit one-sided. Why do you say it? Because there are two ways to implement reference counters, one is embedded (also called intrusive), which directly embeds the reference counter into the object. One is non-embedded ( It can also be called non-intrusive), which is to open up another memory area to store the object.

Reference counter.

It should be mentioned here that the current mainstream virtual machines basically do not use reference counting algorithms to mark whether objects can be recycled (Python's memory management mechanism uses this algorithm, as well as in Redis). Why is it not used? The most important reason The use of reference counting algorithms may result in circular references (A points to B, B points to A) objects that cannot be recycled.

②How to solve the problem that the circular reference object in the reference counting method cannot be recycled?

On Zhihu, someone was asked this during an interview. The answers are divided into three categories: one is to avoid using circular references, one is to use weak references instead of strong references in programs, and the other is to use reference calculation algorithms. The garbage collector + auxiliary tracing GC garbage collector using the "mark-sweep" algorithm to specifically collect circularly referenced objects. We can use it as a reference answer.

 

1.2.2 Reachability Analysis Algorithm

①The reachability analysis algorithm, literally, is to analyze whether the object can still reach the object through a certain line. The line here is the reference chain (Reference Chain), such as A refers to B, B refers to C, start from A to find C ,A-->B-->C This line is the reference chain.

However, the analysis must have a beginning, and this header is the "GC Roots" object in our reachability analysis algorithm.

All in all, we use the "GC Roots" object as the starting point to search down, as long as the corresponding object can be found through the reference chain, then the object is reachable (that is, the object is still referenced and not hung up).

② You may ask again, what kind of objects can be used as "GC Roots" objects? Generally, there are four types of objects that can be used as GC Roots objects:

First, in the Java virtual stack, it is actually the object referenced by the reference type (reference type) in the local variable table in the stack frame;

The second is the object referenced by the static variable defined in the class (such as public static String A="a";).

The third is the object referenced by the constant in the method area

The fourth is the object referenced in the native method call (JNI, Java Native Invocation) in the native method stack.

 

In addition, we did not say at the beginning of 1.2 that the object will be recycled if it is not referenced, but used "may". Why?

Because the object has no reference, it has to go through the marking and filtering process to determine whether the object is to be recycled.

The first marking and screening process: marking the object is not referenced (no matter which algorithm is used), but whether to recycle depends on whether the object has rewritten the finalize() method or finalize() method of the Object class Whether it has been called by the virtual machine. If the object has not rewritten finalize() or has been executed, then the object will be recycled.

If in the screening process, it is found that the object has not executed the finalize() method and is not referenced, then the object will be placed in the F-Quene queue to wait for the finalize() method to be executed, and the virtual machine will create a low priority. The thread named Fianalier goes to execute.

The process of the second mark screening: When the object is put into the F-Quene, the garbage collector will mark it for the second time to see if any object escapes. The escape here means that there is a new reference. The one that has not been re-referenced The object will be recycled. The object that escapes in F-Quene will be directly recycled when it is referenced to 0 again. Pay attention to this.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326176199&siteId=291194637