Java Virtual Machine garbage collection (ii): Garbage Collection Algorithm (reprint)

Source: Internet
Author: User

1. Mark-Sweep algorithm

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

1. Algorithm Ideas

The "Mark-sweep" algorithm is divided into two phases:

(A), tag

       first mark all objects that need to be recycled;

         marking process as described in "Java Virtual machine garbage collection (i) Basics" 2-4, judging object survival or death "-divided into two marking processes (refer to the preceding article for details):

(1), first time tag

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

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

        The object that is necessary to execute the Finalize () method is placed in the F-queue queue;     

(2), second token

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

        in its finalize () method is re-associated with any object on the reference chain, and the second tag moves it out of the collection that is about to be recycled;

        For the first time it is marked, and the second is also marked (if needed but not removed from the collection that is about to be recycled), you can think that the object is dead and can be recycled.

(B), after clearing the

       two tokens, objects that are also in the "about to be recycled" collection will be collected uniformly;

The execution process is as follows:

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 the shortcomings of the improvement 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 tag is cleared;

This results in the allocation of large memory objects and the inability to find enough contiguous memory;

This requires triggering another garbage collection action in advance;

4. Application Scenario

CMS collector for the old age;

2, copy algorithm algorithm

The "copy" (Copying) collection algorithm, in order to solve the efficiency problem of the tag-purge algorithm;

1. Algorithm Ideas

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

(B) When one piece of memory is exhausted, copy the surviving object to another (and use this one);

(C), then the used memory space once cleaned out, and then repeat step 2;

The execution process is as follows:

2. Advantages

This makes it possible to recycle only the entire half of the area;

Memory allocations are also not subject to such issues as memory fragmentation (memory can be allocated using "pointer collisions");

Easy to implement and efficient to operate;

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

3. Disadvantages

(A), wasted space

       reduced available memory to half of the original, too wasteful (solution: can be improved, not by 1 : 1 scale);

(B), efficiency decreases with increased object survival

       More replication is required when the object has a high survival rate. The efficiency will be reduced (fix: The following markup-collation algorithm);

4. Application Scenario

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

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

5. The improved algorithm of hotspot virtual machine

(A), weak agency theory

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

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

(2), few objects will become young generations from the old age;

IBM Research shows that: 98% of the new generation of objects are "dying in the Face of life";

Therefore, it is not necessary to divide the memory by 1:1 ratio (solve the disadvantage 1);

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

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

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

(3), when the recovery, the Eden and the use of survivor in the surviving objects are copied once to another survivor;

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

(5), followed by the use of Eden and copied to the piece of survivor space, repeat step 3;

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

(C) Allocation of security

If another piece of survivor space does not have enough space to hold the last surviving object collected by the new generation, these objects will enter the old age directly through the allocation guarantee mechanism (Handle Promotion);

The allocation guarantee is explained later when the garbage collector executes the rules.

For more information, please refer to: HTTP://DOCS.ORACLE.COM/JAVASE/8/DOCS/TECHNOTES/GUIDES/VM/GCTUNING/GENERATIONS.HTML#STHREF16

3. Marking-Sorting algorithm

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

1. Algorithm Ideas

(1), Mark

The tagging process is the same as the "mark-sweep" algorithm;

(2), finishing

But instead of cleaning up the recyclable objects directly, all the surviving objects are moved to one end;

Then directly clean out the memory outside the end boundary;

The execution process is as follows:

2. Advantages

(A), unlike replication algorithms, efficiency decreases as the object's survival rate increases

Characteristics of the old age:

The object has a high survival rate and no additional space can be allocated for security;

Therefore, the old age generally can not directly choose the algorithm of replication algorithm;

The selection of marker-collation algorithm;

(B), does not like the tag-purge algorithm, resulting in memory fragmentation

Because the cleanup, the survival objects are concentrated on the space side;

3. Disadvantages

The main problem is efficiency: In addition to the labeling process, such as marking-clearing algorithm, there are many processes that need to be collated, less efficient;

4. Application Scenario

Many garbage collectors use this algorithm to reclaim the old age;

such as serial old Collector, G1 (from the overall view);

4, Generational collection algorithm

The "generational collection" (generational Collection) algorithm combines different collection algorithms to process different regions.

1. Algorithm Ideas

Based on the earlier theory of weak generation, there is no new thought;

The memory is divided into several blocks according to the different life cycle of the object.

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

The Java heap is generally divided into the new generation 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), senior generation

The object has a high survival rate and no additional space can be allocated for security;

Use "Mark-clean" or "mark-tidy" algorithm;

Combined with the introduction of memory partitioning in the new generation and the introduction of the Java heap in the previous article, we can draw a general memory partition of the hotspot virtual machine, such as:

2. Advantages

The most appropriate collection algorithm can be used according to the characteristics of each age;

3. Disadvantages

Still cannot control the time of each garbage collection;

4. Application Scenario

At present, the garbage collector of almost all commercial virtual machines adopts generational collection algorithm;

All garbage collectors in the hotspot virtual machine: Serial, Parnew, Parallel scavenge, Serial old, Parallel old, CMS, G1 (also reserved);

5. Train algorithm

The train algorithm, also known as the train algorithm, is a more complete collection algorithm of sub-region processing, which is a powerful supplement to the Generation collection algorithm.

1. Algorithm Ideas

In the train algorithm, the memory is divided into blocks, and multiple blocks form a collection. To visualize, a compartment represents a block, and a train represents a set, such as;

Both the train and the car box are numbered in the order of creation, each compartment is of equal 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 all its compartments ' memory sets;

A collection of memories consists of references to objects in the trunk, which come from objects in a higher-numbered trunk in the same train, and objects in higher numbers;

Garbage collection in the compartment, the overall algorithm flow is as follows:

(1), select the lowest-numbered train;

(2), if the train's memory set is empty, release the whole train and terminate, otherwise proceed to the third step operation;

(3) Select the smallest compartment in the train;

(4), for each element of the compartment memory set:

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

If it is an object pointed to by other trains, 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;

If an object is referenced by an object from multiple trains, it can be copied to any train;

In this step, it is necessary to update the affected collection of references accordingly;

(5), release carriages and terminate;

The collection process will remove some empty boxes and empty cars, and when necessary, will also create some boxes and trains, for more information please refer to: "Compiling principle" second Edition 7.75 "Train Algorithm", "Progressive garbage Collection: Train algorithm";

The execution process is as follows:

2. Advantages

can provide the time-limited asymptotic collection in the mature object space;

Instead of having to do a large area of garbage collection every time;

That is, the time of garbage collection can be controlled, and some small areas are recycled within a specified time;

3. Disadvantages

Implementation is more complex, such as the use of similar algorithms G1 collector in JDK7 only realized;

Some scenarios may not be a good price;

4. Application Scenario

JDK7 after the hotspot virtual machine G1 collector uses similar algorithm, can establish the predictable pause time model;

original bo: 53983064

Java Virtual Machine garbage collection (ii): Garbage Collection Algorithm (reprint)

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.