Detailed explanation of JVM garbage collection algorithm

1 Garbage collection algorithm

There are four garbage collection algorithms:

  • Mark and sweep algorithm
  • Replication algorithm
  • tag sorting algorithm
  • Generational recycling algorithm

1.1 Mark and Clear Algorithm

  • Marking: Traverse the memory area and mark the objects that need to be recycled.
  • Clear: Traverse the memory again and recycle the marked memory.

注:蓝色的为存活对象
Insert image description here
shortcoming:

  • Efficiency issue; the memory space is traversed twice (the first time is marked, the second time is cleared).
  • Space problem: It is easy to generate a large number of memory fragments. When a larger memory is needed, it cannot find a piece that meets the requirements, so the GC has to be started again.

1.2 Replication Algorithm

Divide the memory into two equal-sized chunks and only use one chunk at a time. When a block is used up, or a certain period of time passes, and GC is triggered, the surviving objects in the block are copied to another area, and then the useless memory is cleared at once. The next time the GC is triggered, the surviving parts of that block will be copied to this block, and then that block will be erased, and the cycle repeats.

advantage:

  • Relative to the mark-sweep algorithm, it solves the problem of memory fragmentation.
  • More efficient (when cleaning memory, remember the first and last addresses and erase them at once).

Insert image description here
shortcoming:

The memory utilization rate is not high. Only half of the memory can be used at a time, which is a waste of space.

1.3 Marking sorting algorithm

Because when the survival rate of the object in the previous copy algorithm is relatively high, it is meaningless and a waste of time to keep copying over and over. Therefore, a "marking sorting" algorithm was proposed for the old age.

  • Mark: mark what needs to be recycled
  • Tidying up: Move the surviving objects to one end of the memory, and then clean up the useless memory directly.

As shown in the picture below, you can see that after sorting, the objects are gathered together to free up continuous space.
Insert image description here

Advantages: No fragmentation

Disadvantages: The efficiency of moving the mark forward each time is low.

1.4 Generational recycling algorithm

The essence of the generational recycling algorithm is the combined optimization of the above algorithms. Most commercial virtual machines currently use this generational recycling algorithm. In fact, the life cycle of most objects is very short, so when GC occurs, there are many objects that need to be recycled and very few survive. Therefore, there are very few objects that need to be moved to another memory, so there is no need to divide the memory space 1:1. Instead, divide the entire space into 新生代(1/3) and 老年代(2/3). The new generation is divided into three blocks according to the ratio of 8:1:1. The largest block is called Survivor Area and Eden(伊甸园)区the smaller two blocks are called Survivor Area or Survival Area.To SurvivorFrom Survivor

During the first GC, only the surviving objects of Eden need to be copied to To Survivor. Then the entire Eden area is recycled. When GC is performed again, the surviving Eden and To will be copied to Form Survivor, and the process will be repeated. In this way, the memory available in each new generation accounts for 90% of the entire new generation, greatly improving memory utilization.

However, there is no guarantee that the surviving objects each time will always be less than 10% of the entire new generation. It is possible that the copied objects cannot be saved, so the old generation will provide the final guarantee. If it is not enough, OOM will be thrown.

The structure of the heap space and the detailed recycling process are as follows :

Insert image description here

  • The heap space is divided into the new generation (1/3) and the old generation (2/3). The new generation is divided into eden (8/10), survivor1 (1/10), and survivor2 (1/10).

  • The object is created in eden. If it cannot be placed, minor gc will be triggered. (During minor gc, stop the world will be triggered, other user threads will be suspended, and only the garbage collection thread will work).

  • Objects that survive after an object passes minorGC will be placed in the survivor area, and their age will be +1. If minorGC is executed again, it will be moved to another survivor area. If it cannot be saved during copying, it will enter the old generation.

  • When the age of the object in the survivor area reaches 15, it enters the old age.

  • If the old memory is full. It will try to trigger minor gc first, and then trigger full GC. During Full GC execution, STW takes longer (because there are more survivors in the old generation).

  • If the old generation is full and there is no recyclable garbage, OutOfmemory will be reported.

If the memory of a thread overflows and an exception is thrown (out of memory), other threads will not be allowed to finish running.

This is because when a thread throws an OOM exception, all the memory resources it occupies will be released, which will not affect the operation of other threads and the process will still be normal.

You can open the monitor that comes with jdk to view the memory allocation: execute jvisualvm in the cmd window

The flow chart is as follows:

Insert image description here

Note : It is not only the objects in the survivor area that are over 15 years old that enter the old age, there are many other complex situations.

1.4.1 Conditions for objects to enter the old generation

  • When a larger object is encountered, even if the Eden of the new generation is empty and cannot accommodate the object, it will directly enter the old generation : the size of large objects can be set through parameters, and the size of the object is considered to be a large object.-XX:PretenureSizeThreshold

  • When the object's age reaches 15 years old, it will enter the old age . This age can be set through this parameter:-XX:MaxTenuringThreshold

  • Judging based on the dynamic age of the object, if the total number of objects in the s area exceeds 50% of the s area, then the next time you copy, all the objects whose age is greater than or equal to the maximum age will be put in at once. To the old age.

Insert image description here

  • Old generation space allocation guarantee mechanism: During minor gc, check whether the remaining available space in the old generation is greater than all existing objects in the new generation (including garbage). If it is greater than or equal to, do minor gc. If it is smaller, check whether the guarantee parameter configuration is configured: -XX: -HandlePromotionFailure , if the guarantee is configured, then determine whether the remaining space in the old age is smaller than the average size of objects entering the old age after each minor gc in history. If so, perform full gc directly and reduce one minor gc. If not, execute minor gc. If there is no guarantee mechanism, just full gc.

    Explanation : To put it bluntly, it is necessary to judge whether the remaining space in the old generation can still accommodate the next new generation objects and store them in the old generation.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_43331014/article/details/133849213
Recommended