Java Virtual Machine garbage collection (ii) garbage collection algorithm mark-purge algorithm replication algorithm tag-collation algorithm collection algorithm train algorithm __JVM

Source: Internet
Author: User

Java Virtual Machine garbage collection (ii) garbage collection algorithm

mark-Purge algorithm replication algorithm mark-Collation algorithm generation collection algorithm train algorithm


In the Java Virtual Machine garbage collection (i) basics, learn how to determine whether an object is alive or dead. This paper introduces the basic algorithm of garbage collection: Reference counting algorithm, accessibility analysis algorithm, and some problems of realizing object accessibility analysis in hotspot virtual machine.

Here are some common algorithms for Java Virtual machine garbage collection: Tag-purge algorithm, replication algorithm, tag-collation algorithm, generational collection algorithm, train algorithm, introduce their algorithm ideas, what advantages and disadvantages, as well as the main application scenarios. 1, Mark-elimination algorithm

The tag-purge (Mark-sweep) algorithm is a basic collection algorithm.

1, algorithm ideas

The mark-clear algorithm is divided into two phases:

(A), marking

first Mark all the objects that need to be recycled;

The markup process, as described in the Java Virtual Machine garbage collection (a) Basics, "2-4, Judgment object survival or death"--is divided into two tagging processes (refer to the preceding article for details):

(1), first time marking

After accessibility analysis, it was first marked when the object to GC roots was not connected with any reference chain;

and filter Once: whether this object is necessary to execute the Finalize () method;

Objects that are necessary to execute the Finalize () method are placed in the F-queue queue;

(2), second mark

GC will make a second small scale mark on objects in the F-queue queue;

In its finalize () method, the association is again established with any object on the reference chain, and the second token moves it out of the collection that is about to be reclaimed;

Is marked for the first time, and is marked for the second time (if required but not removed from the collection that is about to be recycled), you can assume that the object is dead and can be recycled .

(B), removal

After two marks, the objects that are also in the "forthcoming collection" will be reclaimed uniformly;

The following diagram of the execution process:

2. Advantages

Based on the most basic of the accessibility analysis algorithm, it is the most basic collection algorithm;

And the subsequent collection algorithm is based on this idea and its deficiencies to improve the obtained;

3. Disadvantages

There are two main disadvantages:

(A), efficiency issues

The efficiency of marking and clearing two processes is not high;

(B) Space issues

A large number of discontinuous memory fragments are generated when the mark is cleared;

This results in the inability to find enough contiguous memory when allocating large memory objects;

In order to trigger another garbage collection action in advance;

4, the application scene

for the old age of the CMS collector; 2, copy algorithm algorithm

"Copy" (copying) collection algorithm, in order to solve the problem of efficiency of tag-purge algorithm;

1, algorithm ideas

(A) to divide the memory into two blocks of equal size, using only one piece at a time;

(B) When a piece of memory is used up, copy the surviving object to another piece (and then use this piece);

(C) to remove the used block of memory once and repeat step 2;

The following diagram of the execution process:

2. Advantages

This makes the memory recovery for the whole half of the region at a time;

Memory allocation does not take into account problems such as memory fragmentation (you can use "pointer collision" way to allocate memory);

Simple to achieve, efficient operation;

(for "Pointer collisions" refer to the Java object creation process in the Hotspot virtual machine)

3. Disadvantages

(A) Waste of space

Available memory reduced to half of the original, too wasteful (solution: can be improved, not according to 1:1 proportion);

(B) efficiency is reduced as the rate of survival of the object increases

When the object survival rate is high, more replication operation is needed, and the efficiency will be reduced (solve: The following tag-finishing algorithm);

4, the application scene

Now the commercial JVM uses this algorithm (by improving the disadvantage of 1) to reclaim the Cenozoic;

such as serial collectors, parnew collectors, Parallel scavenge collectors, G1 (from the local view);

5. Improved algorithm of hotspot virtual machine

(A), weak generation theory

Generational garbage collection is based on weak-generation theory (weak generational hypothesis), which is described as follows:

(1), most of the objects allocated memory will not survive too long, in the younger generation will die;

(2), few objects will be from the old age into a younger generation;

IBM Research shows that: 98% of the new generation of objects are "facing the death";

So there is no need to divide the memory by 1:1 percentage points (solve the disadvantage 1);

(B), hotspot virtual machine generation memory layout and algorithm

(1), the Cenozoic memory is divided into a larger Eden space and two small survivor space;

(2) Each use of Eden and one of the survivor;

(3), when the recovery, will be Eden and the use of the survivor in the same time to copy the object to another piece of survivor;

(4), and then clean out Eden and used survivor space;

(5), after the use of Eden and copied to the piece of Survivor space, repeat steps 3;

The default eden:survivor=8:1, that is, each can use 90% of the space, only a piece of Survivor space is wasted;

(C) Allocation of security

If the other survivor space does not have enough space to hold the surviving objects collected in the last Cenozoic, these objects will enter the old age directly through the distribution guarantee mechanism (Handle promotion);

Distribution guarantee in the future to explain the implementation of the garbage collector rules, further detailed;

More please refer to: Http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/generations.html#sthref16 3, mark-Finishing algorithm

The "tag-collation" (Mark-compact) algorithm is based on the characteristics of the old age.

1, algorithm ideas

(1), marking

The tag process is the same as the tag-purge algorithm;

(2), finishing

But the follow-up is not to clean the recyclable objects directly, but to let all the surviving objects move to one end;

Then directly clean out the memory outside the end of the boundary;

The following diagram of the execution process:

2. Advantages

(A), does not resemble the replication algorithm, the efficiency increases with the object survival rate to become low

Old age Features:

The object survival rate is high, there is no extra space to allocate guarantee;

Therefore, the old age can not be directly selected copy algorithm algorithm;

And the selection of tag-finishing algorithm;

(B), not like Mark-purge algorithm, resulting in memory fragmentation

Because before the clearance, the finishing, the survival objects are concentrated to the space side;

3. Disadvantages

The main problem is the efficiency: in addition to the marking process of image marking-elimination algorithm, there are more processes to be sorted out, and the efficiency is lower;

4, the application scene

Many garbage collectors use this algorithm to recycle old age;

such as serial old collector, G1 (see from the whole); 4, the collection of generation algorithm

The "generational collection" (generational Collection) algorithm deals with different regions with different collection algorithms.

1, algorithm ideas

Based on the previous theory of weak generation, in fact, there is no new thinking;

The memory is divided into several pieces according to the different survival cycle of the object;

In this way, according to the characteristics of each age, the most appropriate collection algorithm can be used;

The Java heap is generally divided into the Cenozoic and the old age;

(A), Cenozoic

Each garbage collection has a large number of objects die, only a small number of survival;

Therefore, the replication algorithm can be used;

(B), older generation

The object survival rate is high, there is no extra space to allocate guarantee;

Use the mark-clean or tag-organize algorithm;

In combination with the introduction of the new generation of memory partitioning and the introduction of the Java heap in the previous article, we can get the hotspot virtual Machine General Memory division, the following figure:

2, the advantages can be based on the characteristics of all ages using the most appropriate collection algorithm;

3, the shortcoming still cannot control each garbage collection time;

4, the application scene

At present, almost all the garbage collectors of commercial virtual machines adopt the algorithm of generational collection.

such as hotspot virtual machine in all garbage collectors: Serial, parnew, Parallel scavenge, serial old, Parallel old, CMS, G1 (also reserved); 5. Train algorithm

Train algorithm, also known as Train algorithm, is a more thorough processing collection algorithm, and is a powerful complement to the collection algorithm of generational generation.

1, algorithm ideas

In the train algorithm, memory is divided into blocks, and multiple blocks form a set. In order to visualize, a compartment represents a block, a train represents a set, as shown below;

Trains and car boxes are all in the order of creation, each compartment is equal in size, but the number of carriages contained in each train is not necessarily equal;

Each car box has a memory set, and the memory set of each train is the sum of the memory sets of all its carriages;

The memory collection consists of references to objects in the trunk, which are from objects in the higher-numbered trunk of the same train and objects in higher sequence numbers;

The garbage collection takes the compartment as the unit, the whole algorithm flow is as follows:

(1), select the smallest marking of the train;

(2) If the memory set of the train is empty, release the whole train and terminate, otherwise carry out the third step operation;

(3) Select the smallest train compartment;

(4), for each element of the memory collection of carriages:

If it is an object referenced by the root reference, it will be copied to a new train;

If it is an object that is pointed at by another train, copy it to the train that points to it.;

Assuming that some objects have been preserved, objects that can be touched by these objects will be copied to the same train;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.