Java garbage Collection algorithm

Source: Internet
Author: User

1. Tag-Clear algorithm

Concept

Marking stage: First, through the root node, all the objects that start from the root node are marked, so the unmarked object is the unreferenced garbage object;

Purge stage: Clears all objects that are not marked.

Disadvantages:

The process of marking and purging is inefficient (both marking and purging need to be facilitated from the beginning to the end)

When a tag is cleared, it produces a large number of discontinuous fragments.

2. Copying algorithms

Concept:

The original memory space atmosphere two pieces, each time just with one piece, when garbage collection, will be used to put in memory of the surviving objects copied into place using the memory block, after meal cleanup is using put memory block put all objects.

Advantages:

This makes every time the entire move and recycling, memory allocation will not be considered memory fragmentation, etc., as long as the mobile heap top pointer in order to allocate memory, simple implementation, high efficiency.

Cons: Waste of space

From the above description is not difficult to see, the replication algorithm to use, the minimum survival rate of the object is very low.

Now the commercial virtual machines are using this mobile phone algorithm to recover the new generation, the new generation of object 98% is to die, so do not need to divide the memory space according to the ratio of 1:1, but the memory is divided into a larger Eden space and two smaller survivor space, Use Eden and one piece of survivor at a time. When recycled, the objects that are still alive in Eden and survivor are copied one at a time into another survivor space, finally clearing up Eden and the survivor space just used. Hotspot virtual Machine The default Eden and Survivor size ratio is 8:1, that is, each new generation can use memory space for the entire Cenozoic capacity of 90%, only 10& space will be wasted.

Of course, 98% of the objects can be recycled only in the general scenario of the data, we have no way to ensure that only a few more than 10% of the object to survive, when the survivor space is not enough, it is necessary to rely on the old age to allocate security, so large objects directly into the old age, the whole process as shown.

Tagging-Sorting algorithm (GC of the old age)

Replication algorithm in the high survival rate of the object to carry out more replication operations, the efficiency will be reduced, so in the old age generally can not directly use this algorithm.

Concept:

Marking phase:

By the root node, all objects starting from the root node are marked, so that objects that are not tagged are garbage objects that are not referenced.

Finishing phase: Compresses all the surviving objects to one end of the memory, then cleans up all the space outside the boundary.

Advantages:

No memory fragmentation occurs.

Disadvantages:

It is also necessary to move objects on the basis of tags, which is relatively expensive and inefficient.

Their difference (> means the former is better than the latter, = it means the same effect)

(1) Efficiency: Copy Algorithm "Mark-sweep algorithm" Mark Clear algorithm (here the efficiency is simply compared to the complexity of time, the actual situation is not necessarily so)

(2) Memory uniformity: Copy algorithm = mark-Organize algorithm mark clear algorithm

(3) Memory utilization: Tag-collation algorithm = tag-purge algorithm copy algorithm

Note that the 1 Mark finishing algorithm can not only compensate for the shortcomings of the memory area dispersion in the mark-and-sweep algorithm, but also eliminate the high cost of half-memory in the replication algorithm.

Note 2: You can see that the tag-purge algorithm is a relatively backward algorithm, but the latter two algorithms are built on this basis.

Note 3: Time and space cannot be combined.

4. Generation of collection algorithms

The current commercial virtual machine GC is a generational collection algorithm, this is not a new idea, just based on the object to put the survival period of the memory divided into a few blocks, generally it is the Java heap is divided into the Cenozoic and the old age: short-lived objects to the new generation, long-term objects to the old age.

Low survival rate: a small number of objects survive, suitable for skin quality algorithm: In the Cenozoic, each GC is found to have a large number of objects died, only a small number of survival (98% of the new generation of objects are facing the birth of the dead), then choose the replication algorithm, only a small amount of survival objects to the cost of replication can be completed GC

High survival rate: a large number of objects survive, suitable for marking-cleaning, labeling-collation; In the old age, because of the high survival rate of the object and the lack of additional space to guarantee his distribution, the GC must be performed using a "mark-clean" or "mark-sweep" algorithm.

Note: In the old age of objects, there is a small part because in the new generation will clean up, the old age to do security, come in, most of the object is because many times GC has not been recycled into the old age.

Java garbage Collection algorithm

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.