Java garbage collection mechanism

1. How to determine if an object is garbage?

  Reference counting method: The implementation is simple and efficient, but if two objects are equal to null at the same time, they will refer to each other, resulting in their reference counts not being 0 and never being recycled.

  Accessibility Analysis:

2. Typical garbage collection algorithm

  Mark-Clear Algorithm: The most basic garbage collection algorithm, with a marking phase and a clearing phase; the task of the marking phase is to mark all objects that need to be recycled, and the task of the clearing phase is to reclaim the space occupied by the marked objects

    Disadvantages: It is easy to generate memory fragmentation. Too much fragmentation may lead to the subsequent process of needing to allocate space for large objects, unable to find enough space and triggering a new garbage collection action in advance

  Copy algorithm: Divide the available memory into two equal-sized blocks according to capacity, and use only one of them at a time. When the memory of one block is used up, copy the surviving objects to another block, and then use the used object. The memory space is cleared at one time, so that memory fragmentation is not easy to occur.

    Disadvantage: The memory that can be used is reduced by half

  Marking-cleaning algorithm: The marking phase is the same as the mark-clearing algorithm. After marking, the surviving objects are moved to one end, and then the memory of the end-boundary accident is cleared.

  

  Generational collection algorithm: Divide the memory into several different areas according to the life cycle of the object, and divide the heap area into the old generation and the new generation. The characteristics of the old generation are that only a small number of objects need to be recycled during each garbage collection, while the new generation The characteristic of the generation is that every time a garbage collection occurs, a large number of objects need to be recycled; the new generation adopts the copy algorithm, and the old generation uses the mark-sort algorithm; there is a permanent generation outside the heap area, which is used to store the class class , constants, method descriptions, etc. The recycling of the permanent generation mainly recycles two parts: abandoned constants and useless classes

 

3. Typical Garbage Collector

  The garbage collection algorithm is the theoretical basis of memory recycling, and the garbage collector is the specific implementation of memory recycling.

 

Guess you like

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