How does the garbage collection mechanism work?

What is the garbage collection mechanism?

Garbage collection is an automatic storage management mechanism. When some occupied memory is no longer needed, it should be released to make room. This storage resource management is called garbage collection.

In other words, garbage collection is related to memory, so what memory is there?

JVM memory

JVM divides memory into five sections.

JVM is divided into five memory spaces. Among them, the program counter, virtual machine stack, and local method stack are generated and destroyed with threads. Therefore, the memory allocation and recovery of these areas are deterministic, and there is no need to pass Consider more about recycling, because when the method ends or the thread ends, the memory will naturally follow the recycling. The Java heap area and method area are different. The allocation and recovery of this part of the memory are dynamic, which is exactly the part that the garbage collector needs to pay attention to.

Garbage collection in heap area

What is rubbish

If an object has no place to reference it, it is garbage.

How to determine it is rubbish

Reference counting

Add a reference counter to the object. Whenever there is a reference to it, the counter is incremented by one. Conversely, every time a reference fails, the counter is decremented by one. When the counter is 0, it means that the object is not referenced.

Accessibility analysis

Set up a number of root objects (GC Root), each object is a child node, when an object can not find the root, it is considered unreachable.

Triggering conditions

Minor GC trigger mechanism

Minor GC will be triggered when the young generation is full, the young generation here refers to the Eden generation full, and the Survivor full will not trigger a GC.

FULL GC trigger mechanism

  1. Insufficient space in the old age
  2. Insufficient method area
  3. The average size of the old generation after passing the Minor GC is greater than the available memory of the old generation.
  4. When copying from Eden area and From Survivor area to To Survivor area, if the size of the object is larger than the available memory of To Space, the object will be dumped to the old generation, and the available memory of the old generation is smaller than the size of the object.
  5. System.gc() method call, the system recommends to implement Full GC, but it is not necessarily implemented.

Recovery algorithm

1. Mark-clear algorithm

The algorithm first marks and then clears, traverses all GC Roots, marks the reachable and unreachable objects respectively, and then reclaims the unreachable objects.

The shortcomings of this algorithm are mainly reflected in efficiency and space

From an efficiency point of view, the two processes of marking and clearing are not efficient, because all GC ROOTs need to be traversed.

From a spatial perspective, a large number of discontinuous memory fragments will be generated after the mark is cleared. Too many memory fragments may lead to the failure to find enough contiguous memory when large objects need to be allocated during the running of the program and have to be triggered in advance. Garbage collection action.

2. Copy algorithm

Divide the memory into two blocks and use only one block each time. When this block of memory is full, copy the surviving objects to another block and arrange them strictly according to the memory address, and then recycle the used memory.

The advantage is: solves the fragmentation problem of the mark-sweep algorithm, and can get continuous memory space. The
disadvantage is: half of the memory is wasted

3. Marking and sorting algorithm

When the object survival rate is high, the continuous replication efficiency of the replication algorithm is very low. The old age is an object that is not easy to be recycled. According to the characteristics of the old age, the tag sorting algorithm can be used. The tag sorting algorithm is based on the mark-sweep algorithm. After marking, do not clean up directly, it is to let all surviving objects move to one end, and then directly clean up the memory outside the boundary. This not only avoids continuous replication when the object survival rate is high, but also avoids the appearance of memory fragmentation. Suitable for Old age.

4. Generational collection algorithm

Modern commercial virtual machines basically use generational collection algorithms for garbage collection. There is nothing special about this algorithm, it is a combination of the above.

It divides the objects in memory according to the length of their life cycle and the difference in the area.

The heap is divided into the new generation and the old generation.

The method area is the persistent generation.

The new generation is an object that will not live long before it will die, usually in the heap memory. For example, local variables.

The old age is a long-lived but also dead object, generally in the heap memory. For example, some objects with a long life cycle.

The persistent generation is an immortal object, generally in the method area. For example, the loaded class information.

Different eras use different garbage collection algorithms.

The new generation uses a replication algorithm.

The old generation uses a tag sorting algorithm.

Garbage collection in method area

The persistent generation is the method area. The Java virtual machine specification says that it is not necessary to require the virtual machine to implement garbage collection in the method area, because compared with the garbage collection efficiency of the heap area, its recycling efficiency is too low, but this part of the memory area It can also be recycled.

The new generation and the old generation of the persistent generation and the heap area are different. The main contents of the method area are discarded constants and useless classes.

Obsolete constants can also be judged by reference reachability, but for useless classes, the following three conditions must be met at the same time:

  • All instances of this class have been recycled, that is, there is no instance of this class in the Java heap;
  • The loading of this class ClassLoaderhas been recycled;
  • The corresponding java.lang.Classobject of this class is not referenced anywhere , and the methods of this class cannot be accessed anywhere through reflection .

Guess you like

Origin blog.csdn.net/numbbe/article/details/109321901