Young Generation and Old Generation Garbage Collection

copy algorithm


For more blog content visit only love to eat dragon fruit , click to learn more


  • overview

The copy algorithm divides the memory into two intervals . At any point in time, all dynamically allocated objects can only be allocated in one of the intervals (called the active interval), while the other interval (called the free interval) is free.
When the effective memory space is exhausted, the JVM will suspend the running of the program and start the copy algorithm GC thread. Next, the GC thread will copy all the surviving objects in the active area to the free area , and arrange them strictly according to the memory addresses. At the same time, the GC thread will update the memory reference address of the surviving objects to point to the new memory address.

  • Advantages of the replication algorithm
  1. To ensure the continuity of the space, there will be no "fragmentation" problem.
  2. No mark and clear process, easy to implement and efficient to run
  • Optimization of the replication algorithm

The memory is divided into 1 Eden area and 2 Survivor areas . The Eden area occupies 80% of the memory space, and each Survivor area occupies 10% of the memory space. For example, the Eden area has 800MB of memory, and each Survivor area has 100MB of memory.
In the Survivor area, one is called From and one is called To, and objects exist in Eden and From blocks. When GC is performed, all surviving objects in Eden are moved to the To block, while in From, the surviving objects are determined according to the age value. When the object reaches a certain value, it will be moved to the old generation, and the object that has not reached the value is copied to the To block. , after GC, Eden and From are emptied. After that, From and To exchange roles, the new From is the original To block, the new To block is the original From block, and the age of the object in the new To block is increased by 1.
Note: The age threshold can be set by -XX:MaxTenuringThreshold

MinorGC process

  • Trigger timing:

It will trigger when the Eden area is full, but the Survivor area will not trigger GC

  • explain

During the running of the code, various objects will be continuously created, and these objects will be preferentially placed in the Eden area and the Survivor1 area of ​​the new generation.
insert image description here

If both the Eden area and the Survivor1 area of ​​the new generation are almost full , Minor GC will be triggered at this time to transfer the surviving objects to the Survivor2 area. At this time, all surviving objects in the Eden area will be transferred to an empty Survivor area at one time. Then the Eden area will be cleared.insert image description here

Then allocate new objects to the Eden area again. There are objects in the Eden area and a Survivor area. The Survivor area contains objects that survived the last Minor GC. If the Eden area is full again next time, Minor GC is triggered again, and the surviving objects in the Eden area and the Survivor area where the surviving objects after the last Minor GC are placed will be transferred to another Survivor area.
insert image description here
insert image description here

When an object enters the old generation

According to target age

Every time an object escapes MinorGC once in the new generation and is transferred to a Survivor area, its age will increase by one year. By default, when the object's age reaches 15 years old, it will avoid 15 At the next GC, it will be transferred to the old generation . The specific age at which a child enters the old age can be set through the JVM parameter "-XX:MaxTenuringThreshold", and the default is 15 years old.
insert image description here

Dynamic Object Age Judgment

There is another rule that allows objects to enter the old age without waiting for 15 GCs to pass.
In the current Survivor area, if the total size of a batch of objects exceeds 50% of the memory size of this Survivor area, then the objects whose age is greater than or equal to the age of this batch of objects can directly enter the old age.
insert image description here

Assume that there are three objects in the Survivor2 area in this figure. The age of these objects is the same, they are all 3 years old, and the combined objects of the two objects exceed 50MB, which is more than half of the 100MB memory size of the Survivor2 area. At this time, the Survivor2 area Objects older than or equal to 3 years old will all enter the old generation. This is the so-called dynamic age judgment rule, and this rule will also allow some objects in the new generation to enter the old age.
In addition, here is a concept to clarify, that is, the actual logic of this rule is as follows: the sum of multiple age objects of age 1+age 2+age n exceeds 50% of the Survivor area. Objects are placed into the old generation .

Large objects go directly to the old generation

There is a JVM parameter, which is " -XX:PretenureSizeThreshold ", which can set its value to the number of bytes, such as "1048576" bytes, which is 1MB. It means that if you want to create an object larger than this size, such as a super large array, or something else, you can directly put this large object in the old generation at this time. will not go through the new generation. The reason for this is to avoid the occurrence of such large objects in the new generation, and then repeatedly avoid GC, and then copy back and forth between the two Survivor areas for many times before entering the old generation.

Too many objects after Minor GC

After the Minor GC, it is found that there are too many remaining surviving objects, and there is no way to put them into another Survivor area. At this time, these objects must be directly transferred to the old generation.
insert image description here
insert image description here

Old Generation Space Allocation Guarantee Rules

  • question leads

If a large number of objects survive in the new generation, it is true that the Survivor area cannot fit them and must be transferred to the old generation, so what if there is not enough space in the old generation to store these objects?

  • solve

First of all, before executing any Minor GC, the JVM will first check whether the available memory space available in the old generation is larger than the total size of all objects in the new generation. To prevent the most extreme situation, all objects may survive the Minor GC in the new generation, and all objects in the new generation will enter the old generation.
If it is found that the memory size of the old generation is larger than all the objects in the new generation, a Minor GC can be initiated on the new generation at this time, because even if all objects survive after the Minor GC, the Survivor area can no longer fit, and they can be transferred to the old generation. s go.
If before executing Minor GC, it is found that the available memory in the old generation is smaller than the size of all objects in the new generation, then at this time, it is possible that all objects in the new generation will survive after the Minor GC, and then all need to be transferred to the old generation, but the old generation The age space is not enough. So if it is before Minor GC, it will check whether a "-XX:-HandlePromotionFailure" parameter is set. If there is this parameter, it will continue to try to make the next judgment.

  • Check whether the memory size of the old generation is larger than the average size of objects entering the old generation after each Minor GC.

For example: after each Minor GC, an average of about 10MB of objects will enter the old generation, so the available memory in the old generation at this time is greater than 10MB. This means that it is very likely that objects of about 10MB will enter the old generation after the Minor GC. At this time, the space in the old generation is sufficient and can be recycled.

  • If the judgment of the above step fails, or the "-XX:-HandlePromotionFailure" parameter is not set, a "Full GC" will be triggered directly at this time, which is to perform garbage collection on the old generation, try to free up some memory space, and then Perform Minor GC.
  • Summarize

Before Minor GC occurs, the virtual machine checks whether the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation.

  1. If greater than, this Minor GC is safe
  2. If it is less than, the virtual machine will check whether the -XX:-HandlePromotionFailure setting value is allowed to fail the guarantee.
    1. If HandlePromotionFailure=true, it will continue to check whether the maximum available continuous space in the old generation is greater than the average size of objects promoted to the old generation
      1. If it is larger, try to perform a Minor GC, but this Minor GC is still risky; if the surviving object is larger than the size of the Survivor area after recycling, and it is also larger than the size of the available memory in the old generation, "Handle Promotion Failure" will occur In this case, a "Full GC" will be triggered at this time
      2. If it is less than that, perform a Full GC instead.
    2. If HandlePromotionFailure=false, perform a Full GC instead
  3. The HandlePromotionFailure parameter is invalid after JDK7. As long as the continuous space of the old generation is greater than the total size of the new generation objects or greater than the average size of the objects promoted to the old generation, MonitorGC will be performed, otherwise FullGC

Old Generation Garbage Collection Algorithm

Markup-Collating Algorithms

The copy collection algorithm needs to perform more copy operations when the object survival rate is high, and the efficiency will be reduced. More importantly, if you don't want to waste 50% of the memory space, you need to provide additional space for allocation guarantees. Since the survival rate of objects in the old generation is relatively high, and no other memory can be found for allocation guarantee , the old generation generally cannot directly use this collection algorithm.
According to the characteristics of the old generation, someone improved the "mark-clear" and proposed the "mark-sort" algorithm. The marking process of the "mark-sort" algorithm is the same as that of the "mark-clear" algorithm, but the subsequent steps do not directly clean up recyclable objects, but let all surviving objects move to one end, and then directly clean up the objects outside the end boundary. memory .
insert image description here

trigger timing

  1. When calling System.gc, the system suggests to execute Full GC, but not necessarily
  2. Insufficient space in the old generation
  3. Space allocation guarantee failed
  4. JDK 1.7 and previous permanent generation (method area) space is insufficient
  5. After Minor GC, the average size of the old generation is larger than the available memory of the old generation
  6. When CMS GC handles floating garbage, if the new generation space is insufficient, the space allocation guarantee mechanism will be adopted, and if the old generation space is insufficient, Full GC will be triggered

Bring a Young GC when the Old GC is executed

  • OldGC trigger conditions
  1. Check before the Young GC occurs. If the "continuous memory space available in the old generation" < "the average size of the total objects promoted to the old generation after the Young GC in the new generation", it means that the objects may be promoted to the old generation after this Young GC The size may exceed the current available memory space of the old generation. At this time, an Old GC must be triggered to make more space for the old generation, and then execute the Young GC
  2. After the execution of Young GC, a batch of objects need to be put into the old generation. At this time, the old generation does not have enough memory space to store these objects. At this time, an Old GC must be triggered immediately (the remaining space of the old generation is greater than the average of the previous young generation entering the old generation. size, but the objects entering the old age after this recycling are much larger than the previous average size)
  3. If the memory usage rate of the old generation exceeds 92%, the Old GC should be triggered directly. Of course, this ratio can be adjusted through parameters.

To put it simply, there is not enough space in the old generation, and no more objects can be placed. At this time, it is necessary to execute Old GC to collect garbage in the old generation.

  • explain
  1. If the Old GC is caused by condition 1, it means that there is insufficient space in the old generation and Young GC cannot be performed. It is necessary to perform Old GC first and then Young GC, so that Old GC occurs before Yonug GC
  2. If it is caused by condition 2, then there is insufficient space after Young GC, which in turn triggers Old GC
  3. In many JVM implementation mechanisms, in fact, when the above conditions are met, it actually triggers Full GC. This Full GC will include Young GC, Old GC, and permanent generation GC.

Guess you like

Origin blog.csdn.net/u010859650/article/details/127975798