laitimes

JVM GC Principle In-depth Analysis and Performance Tuning Practice (II)

Let's take a look at the topology diagram first

JVM GC Principle In-depth Analysis and Performance Tuning Practice (II)

The Java heap can also be subdivided from the perspective of GC: the Cenozoic (Eden, From Survivor and To Survivor zones) and the Old Age.

JVM GC Principle In-depth Analysis and Performance Tuning Practice (II)

Cenozoic: Divided into three zones: one Eden zone and two Survivor zones (in general), most of the objects are generated in the Eden zone. When the Eden zone is full, the surviving objects are copied to one of the two Survivor zones. When this Survivor zone is full, objects that survive and do not meet the "promotion" criteria for this zone are copied to another Survivor zone. Each time a subject goes through a Minor GC, the age is increased by 1, and after reaching the "promotion age threshold", it is placed in the old age, a process also known as "promotion". Obviously, the size of the "promotion age threshold" directly affects the dwell time of the object in the new generation, and in the Serial and ParNew GC recyclers, the "promotion age threshold" is set by the parameter Max TenuringThreshold, with a default value of 15.

Eden Zone: The birthplace of new Java objects (if the newly created objects occupy a lot of memory, they are directly allocated to the old age). When the memory of the Eden region is not enough, the MinorGC will be triggered to perform a garbage collection on the Cenozoic region.

ServivorFrom: Survivor of the last GC, as the scanned person for this GC.

ServivorTo: Survivor of the Minor GC process retained once.

Old Age: Mainly stores memory objects with long life cycles in applications.

The objects of the old age are relatively stable, so the MajorGC will not be executed frequently. Prior to Major GC, MinorGC is usually performed, so that a new generation of objects enter the old age, resulting in insufficient space to trigger. When a large enough contiguous space cannot be found to allocate to a large newly created object, it also triggers a larger MAJORGC in advance to make up space for garbage collection.

MajorGC uses a tag removal algorithm: it first scans all the old ages, marks out the surviving objects, and then recycles the unmarked objects. MajorGC takes longer because it is scanned and recycled. MajorGC will produce memory fragmentation, in order to reduce memory loss, we generally need to merge or mark it out for the next direct allocation. When the old age is also full and can't fit, the OOM (Out of Memory) exception will be thrown.

Permanent generation: refers to the permanent storage area of memory, mainly storing Class and Meta (metadata) information, Class is put into the permanent area when it is loaded, it is different from the area where the instance is stored, the GC does not clean up the permanent area during the main program running period. So this also causes the permanent generation region to swell up as the number of loaded Classes increases, eventually throwing an OOM exception.

The gc process, described briefly, goes like this:

JVM GC Principle In-depth Analysis and Performance Tuning Practice (II)

1, now there is a new object generated, then the object must need memory space, so now need to apply for memory space for the object.

2. First of all, it will be judged whether there is memory space in Eden Park, and if there is sufficient memory space at this time, the new object will be saved directly to Eden Park.

3. However, if the memory space of Eden Park is insufficient at this time, then the Minor GC operation will be automatically performed to clean up the useless memory space of Eden Park. After the cleanup, will we continue to judge whether the Eden Park has enough space? If sufficient, the new object is allocated directly in the Eden Campus for memory space allocation.

4. If the space of Eden Park is still insufficient after the implementation of Minor GC, then the survival area judgment will be carried out at this time, if there is remaining space in the living area, some of the active objects of the Eden Park will be saved in the living area, and then continue to judge whether the memory space of the Eden Park is sufficient, and if it is sufficient, the memory space allocation will be carried out.

5, if there is no memory space in the living area at this time, then continue to judge the elderly area, if the space in the elderly area is sufficient at this time, the active objects in the living area will be saved to the elderly area, and then the survival area should cope with the appearance of spare space, and then the Eden Park will be part of the active objects in the survival area, and finally allocate memory space for new objects in the Eden Park.

6, if the old memory space is also full at this time, then this time will produce Major GC (Full GC). The active objects in the living area are then saved to the old age area to make room, and then some of the active objects in the Eden campus are saved to the living area, and finally the memory space is allocated for the new objects in the Eden park.

7. If the space is still insufficient after the implementation of Full GC in the old age, it should be dealt with OOM (OutOfMemoryError) exceptions.

The maximum memory size is set to 10M, which is convenient for observation, and the result is:

It can be seen that the lack of space in the younger generation triggers the Minor GC, the space that is still insufficient after multiple Minor GC will trigger the space recovery of the old age, the failure of the space allocation in the old generation will trigger the Full GC, and the lack of space after multiple Full GC will lead to "OutOfMemoryErroy".