JVM's garbage collection algorithm [three]

The article summarizes the memory in which objects can be recycled, so to sum up this garbage collector on how to recycle garbage.

For efficient JVM garbage, garbage collection algorithm are summarized as follows:

First, mark - sweep algorithm

Here Insert Picture Description
Clear labeling algorithm is based garbage collection algorithm, simple, butThere will be a serious problem of space debris. Above, if at this time to create a 3M-sized objects, but not> = 3M contiguous memory space, GC occurs.

Second, replication algorithm

Here Insert Picture Description
The algorithm is based on the algorithm flag clearing up, the first memory area divided into two parts, one of which uses only. When one of which memory usage when finished, will live objects copied to another, and then empty this one. Thus each time for the entire half-area memory recall, do not consider the complexities of memory fragmentation etc., as long as the top of the stack pointer moves sequentially assigned to, simple, efficient operation. The algorithm solves the problem of space debris mark sweep algorithm memory, but memory usage is only 50%.

The current mainstream commercial virtual machines are based on the replication algorithm to recover the new generation, the new generation of 98% of the objects are "raw evening toward death", so no 1: 1 memory space is divided, but divided into 8: 1 : Eden area and two small areas Survivor 1, wherein each use a Survivor Eden region and zone. If no object under another piece on enough space to store a Minor GC survival Survivor areas, these objects directly through the allocation guarantee mechanism (like a good credit and pay back the money on time every time, if not on a secured people, here's the equivalent of the old guarantor) into the old era.

Third, the mark - Collation Algorithm

Here Insert Picture Description
Collation Algorithm mark mark mark and sweep algorithm process as just before the first live objects move clear to one end, and then clear the garbage objects. The algorithm solves the problem of tags to organize algorithm memory space debris problem algorithms and copy space utilization low. However, the algorithm needs to organize all live objects memory address in the finishing process, so the efficiency is very low.

Fourth, generational collection algorithm

Generational collection algorithm is actually a combination of the above three algorithms under certain circumstances. Corresponding to heap This memory area is divided into the old and the new generation's, which use the new generation of replication algorithm, using old's mark - sweep or mark - sorting algorithms.

Memory models:

Here Insert Picture Description
Old and new generation's default size ratio of 1: 2, Eden s0 and s1, and the ratio is 8: 1: 1, can modify the parameters.
Here Insert Picture Description

Survivor areas of action is to play the role of a buffer, if not this part, every Minor GC, Eden survive the object will move to the old years old years will soon be filled.

Long STW happens when Old zone occurred Full GC (- the - Stop world), Old District, the more memory the longer time-consuming, so the Old memory is not the bigger the better. Old district has a large number of 'old fool objects', because there is no memory area Old guarantee policy, if replication algorithm, it is a waste of space, it can easily lead to OOM, so the use of mark - sorting algorithms.

Object allocation process:
In fact, most of them are in-memory objects raw evening toward death, survival time is very short. New objects are generally created will allocate memory space Eden area, if Eden District insufficient memory space, JVM will conduct a Minor GC, Eden area and s0 or s1 empty, Eden area and s0 or s1 that area cleared of live objects moving to that area s0 or s1 idle, the Age + 1 s area moving over the live objects can not let go (due to the allocation guarantee mechanism) and move objects reached the age defaults to 15 years old. Eden then if still fit the object, indicating that a large object, the object will be directly into the old era, if does not fit the old years, JVM will be Full GC, then if the store no less, OOM occurs, it is stored under deposit.
FIG :( follows from the network)
Here Insert Picture Description

There are three objects will enter the old year:

Large objects
large objects refer to objects requires a lot of contiguous memory space. -XX: PretenureSizeThreshold can set the size of a large object, more than the size of the object is large, this is only a small parameter in Serial and ParNew two collectors.

Long-term survival target
virtual machine defines a target age for each object (Age) counter (in the subject header). Objects will continue in the area between the From and To Survivor region move under normal circumstances, the object in each area experience a Survivor Minor GC, increased age, 1 year old. As the age increases to the age of 15, this time will be transferred to the old era. Age can JVM parameter -XX: to set MaxTenuringThreshold.

Age dynamic object
virtual machine does not require that the object must be aged 15 years old, will put older area, if space Survivor certain age or less (including the age) is greater than half the total size of the object Survivor space, then aged greater than or equal to the the target age you can go directly to the elderly area, dynamic mechanism triggered after the age of Minor GC.

Published 11 original articles · won praise 0 · Views 614

Guess you like

Origin blog.csdn.net/fei1234456/article/details/104736912