JVM Series 3--Garbage Collection and Algorithms

  1. How to determine trash
    1. reference counting
              In Java, references and objects are related. If you want to manipulate objects, you must use references. Therefore, it is obvious that a simple way is to use reference counting to determine whether an object can be recycled. Simply put, if an object does not have any references associated with it, that is, their reference counts are not 0, it means that the object is unlikely to be used again, then the object is a recyclable object.
    2. accessibility analysis
              In order to solve the circular reference problem of reference counting method, Java uses reachability analysis method. Search through a series of "GC roots" objects as a starting point. An object is said to be unreachable if there is no reachable path between "GC roots" and the object. It should be noted that unreachable objects are not equivalent to recyclable objects. Unreachable objects must go through at least two marking processes to become recyclable objects. If it is still a recyclable object after being marked twice, it will face recycling.
  2. Mark Sweep Algorithm ( Mark-Sweep )

    From the figure, we can find that the biggest problem of this algorithm is serious memory fragmentation, and subsequent problems may occur where large objects cannot find available space.
     

    1. The most basic garbage collection algorithm is divided into two stages, marking and clearing. The marking phase marks all objects that need to be recycled, and the cleaning phase reclaims the space occupied by the marked objects. As shown in the picture
  3. Copying algorithm ( copying )

           An algorithm proposed to solve the memory fragmentation defect of the Mark-Sweep algorithm. Divide the memory into two blocks of equal size according to the memory capacity. Only use one block at a time. When this block of memory is full, copy the surviving objects to another block and clear the used memory, as shown in the figure:

    Although this algorithm is simple to implement, has high memory efficiency and is not prone to fragmentation, the biggest problem is that the available memory is compressed to half of the original size. And if the number of surviving objects increases, the efficiency of the Copying algorithm will be greatly reduced.

  4. Mark Compact Algorithm (Mark-Compact)

    A combination of the above two algorithms is proposed in order to avoid defects. The marking phase is the same as the Mark-Sweep algorithm. After marking, the objects are not cleaned up, but the surviving objects are moved to one end of the memory. Then clear objects outside the end boundary. As shown in the picture:
  5. Generational collection algorithm
    1. The generational collection method is currently used by most JVMs. Its core idea is to divide the memory into different domains according to the different life cycles of the objects. Generally, the GC heap is divided into the Tenured/Old Generation and Young Generation. The characteristic of the old generation is that only a small number of objects need to be recycled in each garbage collection. The characteristic of the new generation is that a large amount of garbage needs to be recycled in each garbage collection. Therefore, different algorithms can be selected according to different areas.
    2. New generation and replication algorithm

      At present, most JVM GCs adopt the Copying algorithm for the new generation, because most objects must be recovered during each garbage collection in the new generation, that is, there are relatively few copy operations, but the new generation is usually not divided according to 1:1. Generally, the new generation is divided into a larger Eden space and two smaller Survivor spaces (From Space, To Space). Each time the Eden space and a piece of Survivor space are used, when recycling is performed, the two spaces The surviving objects are copied to another Survivor space.
    3. Old generation and marked replication algorithm
       

      The old generation only recycles a small number of objects each time, so it uses the Mark-Compact algorithm.

      1. The Permanent Generation (Permanet Generation) in the method area mentioned by the JAVA virtual machine is used to store classes, constants, method descriptions, etc. The recycling of the eternal generation mainly includes discarded constants and useless classes.

      2. The memory allocation of objects is mainly in the Eden Space of the new generation and the From Space of the Survivor Space (the block where the Survivor currently stores objects). In a few cases, it will be allocated directly to the old generation.

      3. When the Eden Space and From Space of the new generation are insufficient, a GC will occur. After the GC, the surviving objects in the Eden Space and From Space areas will be moved to To Space, and then the Eden Space and From Space will be cleaned up.

      4. If To Space cannot store an object enough, the object will be stored in the old generation.

      5. After GC, Eden Space and To Space are used, and the cycle is repeated.

      6. When an object escapes a GC in the Survivor area, its age will be +1. By default, objects whose age reaches 15 will be moved to the old generation.

        

Guess you like

Origin blog.csdn.net/weixin_38340874/article/details/122082316