Garbage Collection - Generational Collection

Generational Garbage Collection

In "Garbage Collection Algorithms", three garbage collection algorithms are introduced, but the actual JVM virtual machine does not use a certain algorithm alone. It combines the previous three algorithms to work together. The specific implementation is called in the virtual machine. It is a generational garbage collection mechanism, which divides the entire area of ​​our heap memory into two parts, one is called the new generation and the other is called the old generation. The new generation is further divided into the Garden of Eden, the Survival Area From and the Survival Area To.

Why do such a regional division? The main reason is that some objects in Java need to be used for a long time. Objects that have been used for a long time are placed in the old generation, and those objects that can be discarded after use can be placed in the new generation. Different characteristics of the life cycle carry out different garbage collection strategies. Garbage collection in the old generation happens only once a long time, while garbage collection in the new generation occurs more frequently. The new generation deals with objects that are about to die, while the old generation deals with objects that are more valuable and exist for a long time. In this way, using different algorithms for different areas can manage garbage collection more effectively.

When we create a new object, the new object will use a space in the Garden of Eden by default, and then many objects may be created and placed in the Garden of Eden, and the free space in the Garden of Eden will gradually decrease. When an object is created and found in the Garden of Eden When there is not enough space, a garbage collection will be triggered, which is in the new generation of garbage collection (Minor GC). After the Minor GC is triggered, the reachability analysis algorithm mentioned earlier will be used to search along the GC Root reference chain to see whether the objects in the Garden of Eden are useful or garbage and mark them. After the mark is successful, the copy algorithm will be used to copy the surviving objects to the surviving area To. After copying to the surviving area TO, the life of the surviving objects will be increased by 1, and the remaining objects in the Garden of Eden can be fully recycled, and then Let the survival area From and the survival area To be exchanged, so far the first garbage collection is completed, and at this time there is enough space in Eden, and you can continue to allocate objects to Eden.

After a period of time, the space in Eden is full again, and the second garbage collection is triggered. In addition to finding the surviving objects in Eden, this garbage collection also needs to find out whether there is any need to continue to survive in the surviving area. Object. Then put the surviving objects in the Garden of Eden into the survival area TO and increase the lifespan by 1. In addition, put the surviving objects in the survival area From in the survival area To and add 1 to the previous life span . Then come back to free Eden and clean up the garbage from the survivor area. Then exchange From and To. In this way, there is enough space in the Garden of Eden, and the second garbage collection is completed.

Objects in the survival area will not stay in the survival area forever. When its life span exceeds a threshold, for example, the default threshold is 15, that is, as long as the object is still alive after 15 garbage collections, it means that the value of the object is relatively high , is often used, so there is no need to keep it in the survivor area, because the object will not be reclaimed by garbage collection in the future. At this time, the object will be promoted to the old generation, because the frequency of garbage collection in the old generation is relatively low. It will not be easily recycled. This is the process of Minor GC garbage collection.

During the continuous recycling process, it may appear that the memory space in the old generation is full, and the space in the new generation is also full. At this time, Minor GC cannot solve the problem. At this time, Full GC is required. Generally, these garbage Recycling is only triggered when there is insufficient memory in the space. The meaning of Full GC is to do an overall cleanup when the space in the old generation is insufficient. From the new generation to the old generation, the entire heap is cleaned up.

Summarize

  • Objects are first allocated in the Eden area.
  • Insufficient space in the new generation, Minor GC is triggered, the surviving objects of Eden and From are copied to TO using Copy, the age of surviving objects is increased by 1 and from to is exchanged
  • Minor GC will cause Stop the world (when executing the garbage collection algorithm, all other threads of the Java application are suspended (except the garbage collection helper)), the reason for suspending the threads of other users is because in the garbage The process of recycling involves the copying of the object, that is, the change of the object address. In this case, if multiple threads are running at the same time, it will cause confusion. The object has been moved, and other threads access this object again. According to the original The address cannot find this object, so STW will be triggered. Suspend other user threads, and wait for the garbage collection to complete before the user thread can continue.
  • When the life of the object exceeds the threshold, it will be promoted to the old generation, and the maximum life is 15 times (4bit).
  • When there is not enough space in the old generation, it will try to trigger Minor GC first, and if there is still not enough space in the future, it will trigger a Full GC. FULL GC will also cause STW, but the STW time corresponding to the old generation is longer.

Guess you like

Origin blog.csdn.net/qq_35363507/article/details/104930159