[JVM Series] Detailed Explanation of Memory Allocation and Recovery Strategies


Minor GC and Full GC

  • Minor GC: Recycle the new generation, because the survival time of the new generation objects is very short, so Minor GC will be executed frequently, and the execution speed will generally be faster.

  • Full GC: Recycle the old generation and the new generation. The old generation objects have a long survival time, so Full GC is rarely executed, and the execution speed will be much slower than Minor GC.

memory allocation strategy

When we new an object instance, it is first stored in the Eden Park in the new generation of the heap. If the space in the Eden Park is full, YGC will be performed. This article will describe how the object allocation process is...

general process

First light GC

image-20210615215811323.png

  1. The new object is placed in the Eden area first;
  2. When the Eden Park space is full and the program wants to create new objects, the JVM garbage collector will perform garbage collection on the Eden Park, destroy the objects in the Eden Park that are no longer referenced by other objects, and then load new objects to Eden Park;
  3. Then move the remaining objects in the Eden area to the survivor area 0. Each object is assigned an age counter (age). Every time a GC is performed, the age of the surviving objects is accumulated, and the age of the above two surviving objects is assigned a value of 1. ;
  4. After the GC is completed, the Eden area is empty at this time.

说明:

  1. The S0 area and the S1 area are also called the From area and the To area. It is very simple to judge the two areas. Whoever is empty and who is To, the other is the From area;
  2. Every time GC occurs, the surviving objects will be put into the To area. Take the above as an example. During the first GC, S0 is the To area. After the GC is completed, the S1 area is empty and becomes the To area;

Second light GC

image-20210615221650327.png

  1. When the space in the Eden Park is full again, the second GC is performed, and the surviving objects in the Eden Park are put into the S1 area, and the age is 1 at this time;

  2. At the same time, it is judged whether the object in the S0 area is still in use, and if it is used, it is put into the S1 area, and the accumulated age is 2 at this time;

  3. After the second GC is completed, the S0 area is empty. At this time, the S0 area is the To area, and the S1 area is the From area;

Nth GC

image-20210615223737583.png

  1. During the Nth GC, we found that the age of two objects in the S1 area is already 15. If these two objects are still referenced at this time, they will be promoted to the Old area;
  2. Among them, 15 is the default threshold, which can be set by yourself:-XX:MaxTenuringThreshold=<N>

special process

image-20210615232416390.png

  1. When creating a new object, first judge whether the Eden area can be placed, if it can be placed, it will allocate memory for it, if it cannot be placed, it will perform YGC;
  2. Then judge whether the Eden area can be placed. If it can be placed at this time, allocate memory for it. If it still cannot be placed, it means that the object is larger than the space in the Eden area. This object is a super large object. It is placed in the Old area. If the Old area cannot be placed, FGC will be performed, and then it will be judged whether the Old area can be placed. If it is placed, there will be an Old area. If it still cannot be placed, an OOM exception will occur;
  3. During YGC, the surviving objects will be put into the Survival Area. At this time, it is judged whether they can be put down. If the Survival Area cannot be put down, they will be directly put into the Old Area.

summary

  1. When does GC happen?

    • When the Eden Park is full, GC will be performed, and when the Survival Area is full, GC will not be performed. Only when the Eden Park is full, the Survival Area will be passively GCed.
  2. About survivors S0 and S1 areas: there is an exchange after copying, whoever is empty is To;

  3. For Garbage Collection: Frequent collection in young area, rarely in old area, almost never in permanent area/metaspace.

Trigger conditions for Full GC

For Minor GC, its trigger condition is very simple, when the Eden space is full, a Minor GC will be triggered. The Full GC is relatively complicated, with the following conditions:

1. Call System.gc()

It is only recommended that the virtual machine execute Full GC, but the virtual machine does not necessarily execute it. This approach is not recommended, instead let the virtual machine manage the memory.

2. Insufficient space in the old generation

Common scenarios where there is insufficient space in the old generation are the large objects mentioned above directly entering the old generation, and objects that have survived for a long time entering the old generation.

In order to avoid the Full GC caused by the above reasons, you should try not to create too large objects and arrays. In addition, you can use the -Xmn virtual machine parameter to increase the size of the new generation, so that objects are recycled in the new generation as much as possible and do not enter the old generation. You can also use -XX:MaxTenuringThreshold to increase the age at which the object enters the old generation, so that the object can survive for a longer period of time in the new generation.

3. Space allocation guarantee failed

Minor GC using the copy algorithm needs the memory space of the old generation as a guarantee, and if the guarantee fails, a Full GC will be executed. Please refer to Section 5 above for details.

4. JDK 1.7 and previous permanent generation space is insufficient

In JDK 1.7 and before, the method area in the HotSpot virtual machine is implemented by the permanent generation, which stores some Class information, constants, static variables and other data.

When there are many classes to be loaded, reflected classes, and methods to call in the system, the permanent generation may be full, and Full GC will also be executed if it is not configured to use CMS GC. If it still cannot be recycled after Full GC, then the virtual machine will throw java.lang.OutOfMemoryError.

In order to avoid the Full GC caused by the above reasons, the available methods are to increase the permanent generation space or switch to CMS GC.

5. Concurrent Mode Failure

During the execution of CMS GC, there are objects to be put into the old generation at the same time, and the space in the old generation is insufficient at this time (probably due to too much floating garbage during the GC process, resulting in temporary insufficient space), a Concurrent Mode Failure error will be reported and trigger Full GC.

Guess you like

Origin blog.csdn.net/jiang_wang01/article/details/131173505