JVM learning (2) - JVM garbage collection algorithm

When Java virtual collects garbage objects, a copy is divided into two steps:
1. Before the virtual machine reclaims the heap space, it will first determine which objects have died;
2, and then recycle these dead objects through a specific recycling algorithm
 
1. Determine whether the object is dead
1. Citation calculator algorithm
Add a reference counter to an object, increment it by 1 if it is referenced elsewhere, and decrement it by 1 when the reference expires. When the counter is 0, the object cannot be used again and needs to be recycled.
Advantage: easy to implement
Disadvantage: Can't solve the problem of mutual circular reference between objects.
 
2. Root search algorithm GC Roots Tracing
Through a series of objects named GC Roots as the starting point, the search starts from these nodes, and the search path is called the reference chain. When an object is not connected to the GC Roots by any reference chain, it is unreachable (Fig. On Dijkstra's Algorithm), prove 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)
  • Objects referenced by class static properties 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 native method stack
ps: A second judgment will be made. After the object is judged to be a dead object, the virtual machine will place the object in the F-QUEUE queue, and then start a thread to scan. If it is found that it is still not referenced after scanning, it will be recycled directly.
 
 
2. Garbage Collection Algorithm
1. Mark-Sweep Algorithm
It is divided into two stages: marking and clearing. First, the objects that need to be cleared are marked. After the marking is completed, all marked objects are uniformly recycled.
Disadvantages: low efficiency, resulting in more fragments
 
2. Replication algorithm
Divide the available memory into two equal parts, using only one block at a time. When this piece of memory is used up, all surviving objects are copied to another memory block, and then this piece of memory is cleared all at once.
Many virtual machines now use this method to recycle the new generation. The new generation is divided into a large Eden space and two small Survivor spaces. Eden and one Survivor are used each time. When recycling, copy all the surviving objects of Eden and Survivor to another piece of Survivor space at one time, and finally clean up Eden and the Survivor space just used. If the Survivor has insufficient space, it will borrow some memory from the old generation.
Advantages: simple and efficient, and the fragmentation rate is relatively low.
 
3. Marking-collating algorithm
If the survival rate is relatively high, then using the replication algorithm is not suitable.
Move all live objects to the front end, and then directly clean up the content outside the end boundary. 
Use for old age.
 
4. Generational collection algorithm
Today's virtual machines generally use this method.
The java heap is divided into the new generation and the old generation, the new generation adopts the copy algorithm, and the old generation adopts the mark-collation algorithm.

 

Guess you like

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