JVM - Garbage Collector (1) - How to judge that an object is "dead"

content:

1. Why do you need to understand the garbage collection mechanism?

2. Which types of memory need to be garbage collected?

3. How to judge the object is "dead"? - two algorithms

4. The breakdown of citations

5. The subject's self-rescue

 

 

1. Why do you need to understand the garbage collection mechanism?

When we need to troubleshoot various memory overflows and memory leaks, and when garbage collection becomes a bottleneck for the system to reach higher concurrency, we need to implement necessary monitoring and adjustment for these "automated" technologies.

 

2. Which types of memory need to be garbage collected?

In the Java runtime memory, the program counter, the virtual machine stack, and the local method stack are private to the thread, and they are created and destroyed with the thread. The Java heap is different from the method area. The memory required by the implementation class may be different, and the memory required by multiple branches in a method may also be different. We can only know which object will be created when the program is running. The allocation and recovery of this part of the memory are dynamic. The garbage collector is also concerned with this part of the memory.

 

3. How to judge the object is "dead"?

Reference counting algorithm :

To record the number of times an object is referenced, when the reference counter is 0, it means that the object is no longer used.

Advantages: simple to implement, and high judgment efficiency.

Disadvantage: It is difficult to solve the problem of circular references between objects.

 

Reachability Analysis Algorithm:

The mainstream implementations in mainstream commercial programming languages ​​use reachability analysis to determine whether an object is alive. The basic idea of ​​this algorithm is to use a series of objects called "GC Roots" as the starting point, and start searching downward from these nodes, and the path traversed by the search is called the Reference Chain. When Roots does not have any references connected, it proves that this object is not available.

In the java language, objects that can be used as GC Roots include the following:

① Objects referenced in the virtual machine stack (local variable table in the stack frame).

②The object referenced by the static property in the method area.

③ The object referenced by the constant in the method area.

④ The object referenced by JNI (that is, the native method in general) in the local method stack.

 

4. Quoted breakdown:

The definition of reference in java is very traditional: if the value stored in the data of reference type represents the starting address of another piece of memory, it is said that this piece of memory represents a reference. This definition is very pure, but it is too narrow. Under this definition, an object has only two states of being quoted or not being quoted. It is powerless to describe some objects that are "tasteless to eat, and a pity to discard".

We want to describe a set of objects that can be kept in memory when there is enough memory space, and can be discarded if memory space is still very tight after garbage collection.

After JDK1.2, java expanded the concept of reference, and divided the reference into four types: Strong Reference, Soft Reference, Weak Reference, and Phantom Reference.

Strong reference : Refers to the ubiquitous reference in the program code, such as "Object obj=new Object()", as long as the strong reference is still there, the garbage collector will never reclaim the referenced object.

Soft reference : It is used to describe some objects that are useful but not necessary. For the objects associated with soft references, before the memory overflow exception will occur in the system, these objects will be listed in the recycling range for the second recycling. If there is not enough memory for this recycling, the memory will be thrown out. Overflow exception.

Weak reference : It is also used to describe non-essential objects, but its strength is weaker than soft references and can only survive until the next garbage collection.

Virtual reference : The weakest reference relationship, which will not affect its lifetime at all, and cannot obtain an instance of an object through a virtual reference. The sole purpose of setting a phantom reference to an object is to receive a system notification when the object is reclaimed by the collector.
 

5. The subject's self-rescue

Even objects that are unreachable in the reachability analysis algorithm are not "necessary to die". At this time, they are temporarily in the "probation" stage. To truly declare an object dead, it must go through at least two marking processes: if the object After the reachability analysis, it is found that there is no reference chain connected to the GC Roots, then it will be marked and screened for the first time. The screening condition is whether it is necessary to execute the finalize() method on this object.

When the object does not override the finalize() method, or the finalize() method has been called by the virtual machine (the object has only one chance to save itself), the virtual machine regards both cases as "unnecessary to execute".

If the object is determined to be necessary to execute the finalize() method, then the object will be placed in a queue called F-Queue, and later by a low priority automatically created by the virtual machine. Finalize thread to execute it. The so-called "execution" here means that the virtual machine triggers this method, but does not promise to wait for it to run to the end. The reason for this is that if an object executes slowly in the finalize() method, or an infinite loop occurs, it is likely Other objects returning to the lotus juice F-Queue queue are permanently in a waiting state, and even cause the entire memory recovery system to crash.

The Finalize() method is the last chance for the object to escape the fate of death. Later, the GC will mark the objects in the heap F-Queue for a second small-scale mark. If the object is to successfully save itself in finalize() - it only needs to re- It can be associated with any object in the reference chain. For example, assign this to a variable or a member variable of an object. When it is marked for the second time, it will be removed from the "to be recycled" collection; if the object is at this time And escape, then basically he was really recycled.

Guess you like

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