JVM garbage collection algorithm illustration

Reference document: https: //www.toutiao.com/a6691966641242112516/

Garbage collection algorithm

Mark - sweep method

Mark - sweep algorithm from the root set (GC Roots) scan of the survival of the subject labeled, after labeling is completed, then the entire scanned object space unlabeled, recovered, as shown in FIG.

Mark - sweep algorithm does not require moving object, the object is not just to survive treated in more live objects situation is extremely efficient. However, due to mark - sweep algorithm directly recovered objects do not survive, it will cause memory fragmentation.

Replication algorithm

Copy the proposed algorithm is to overcome the overhead handle and solve the problem of memory fragmentation.

It starts when the stack is divided into a plurality of free surfaces and the target surface, the target surface from the target program to allocate space when the object is full, copying garbage collection algorithm based on the root set (GC Roots) scanning active objects, and copies each active object to the free surface (that there is no free memory hole between active objects share). Such free surface becomes a target surface, the original object surface becomes a free surface, the program will allocate memory in the new object plane.

Mark - Collation Algorithm

Mark - sorting algorithm mark - sweep algorithm same way mark object, but they differ in the removal, after the object space occupied by the recovery nonviable, will all live objects moves to the left end of the free space, and updates the corresponding pointer.

Mark - Collation Algorithm in mark - sweep algorithm based on, has conducted a moving target, and therefore more costly, but it solves the problem of memory fragmentation.

DETAILED process shown below:

Generational collection algorithm

Generational collection algorithm is an algorithm currently most JVM's garbage collector uses. Its core idea is based on the object alive the memory of the life cycle is divided into several different areas.

Under normal circumstances the heap area into the old year (Tenured Generation) and the new generation (Young Generation), in addition to the heap area there is a generation of permanent-generation (Permanet Generation).

The characteristics of old age is a time only a few objects need to be recycled garbage collection, and the characteristics of the new generation that has a large number of objects that need to be recovered each time garbage collection, then you can take the most appropriate collection according to the characteristics of different generations algorithm.

The young generation collection algorithm

a) First of all newly generated objects are on the younger generation. The young generation's goal is to quickly collect those that fall short of the life cycle of the object as possible.

b) the new generation memory in accordance with 8: 1: 1 ratio into a eden region and two survivor (survivor0, survivor1) region.

Most object generation region in Eden, the first region eden live objects copied to a recovery zone survivor0, then emptied eden region.

When this survivor0 area also hosts the full, the district will eden region and survivor0 surviving copy objects to another survivor1 area, and then empty the eden survivor0 this area, at this time survivor0 area is empty

Then survivor0 region and survivor1 exchange region, i.e. survivor1 holding area is empty, and so forth.

c) When survivor1 zone is not sufficient to store the object eden survival and survivor0, it will be live objects placed directly on to the old era.

If it's also full of old would trigger a Full GC, which is the new generation, the old year are recycled.

GC d) the occurrence of new generation, also known as Minor GC, MinorGC relatively high frequency of occurrence (not necessarily the Eden area is full and so we have to trigger).

Old generation collection algorithm

a) experienced the object N times the garbage after the recovery is still alive in the young generation, the older generation will be put.

Therefore, it is considered the old generation are stored in some of the longer life cycle of the object.

b) a lot more memory than the new generation of large (probably the ratio is 1: 2), that is triggered Major GC Full GC years old when the memory is full, Full GC occurrence frequency is relatively low, the old target's survival time is relatively long, high survival mark .

Permanent generation collection algorithm

Used to store static files, such as Java classes and methods. Permanent generation no significant effect on garbage collection, but some applications may call some or dynamically generated class, such as Hibernate, etc.,

At such times we need to set a relatively large permanent generation space to store these processes run in the new class.

Also known as the permanent generation method area.

When GC is triggered

In general, when a new object is generated, and upon failure of Eden space applications, it will trigger Scavenge GC, the GC area of ​​Eden, remove non-live objects, and the object moving to yet survive Survivor areas. Then come up with two zones of Survivor.

GC this way for younger generations of Eden zone, will not affect the old generation. Because most of the objects are from the beginning of the Eden area, while the Eden area does not allocate large, so the GC Eden area occur frequently.

Thus, in general use here need fast speed, high efficiency of the algorithm, the Eden to be able to free up as soon as possible.

Scavenge GC

In general, when a new object is generated, and upon failure of Eden space applications, it will trigger Scavenge GC, the GC area of ​​Eden, remove non-live objects, and the object moving to yet survive Survivor areas. Then come up with two zones of Survivor.

GC this way for younger generations of Eden zone, will not affect the old generation. Because most of the objects are from the beginning of the Eden area, while the Eden area does not allocate large, so the GC Eden area occur frequently.

Thus, in general use here need fast speed, high efficiency of the algorithm, the Eden to be able to free up as soon as possible.

Full GC

The whole heap sort, including Young, Tenured and Perm.

Full GC because of the need to recover the entire heap, so slower than Scavenge GC, and should therefore reduce the number of Full GC as possible.

In the process of tuning the JVM, a large part of the work is adjusted to the Full GC.

The following reasons may lead to Full GC:

  • Years old (Tenured) to be filled;
  • Permanent generation (Perm) are full;
  • System.gc () call is displayed;
  • Dynamic changes since the last GC Heap of each domain allocation strategy;

 

Guess you like

Origin www.cnblogs.com/fxl-njfu/p/11424143.html