Based on common JVM garbage collection algorithms, jvm garbage collection

Source: Internet
Author: User

Based on common JVM garbage collection algorithms, jvm garbage collection

There are four common garbage collection algorithms in JVM:

Mark-clearing algorithm (Mark-Sweep );

Copying );

Mark-Compact );

Generational collection;

Next we will introduce them one by one:

I. Mark-clearing algorithm (Mark-Sweep)

This is the most basic garbage collection algorithm. The algorithm is divided into two stages: "mark" and "clear": first, mark all objects to be recycled, after the tag is complete, all marked objects are recycled. There are two main disadvantages: efficiency, low tag and clearing efficiency, and space. After marking is cleared, a large number of discontinuous memory fragments are generated, too many space fragments may cause the allocation of large objects to fail to have enough continuous space. Instead, you have to trigger another garbage collection action in advance.

Mark-clear Algorithm

Ii. Copying)

To solve the efficiency problem, the "copy" algorithm divides the available memory into two blocks of the same size. Each time only one piece is used up, when one piece of space is used up, the surviving objects are copied to the other, and the memory space that has just been used is cleared once. In this way, the memory is recycled every time, and the memory fragments and other complex situations do not need to be considered during memory allocation. Easy to implement and efficient to run. The cost of this algorithm is to reduce the memory to half of the original size, which is too expensive. In fact, 98% of objects in the new generation are killed overnight, so we do not need to divide the memory, instead, the memory is divided into a large Eden space and two smaller memory vor spaces. Each time the Eden space is used and one memory vior space is used. When recycling, copy the surviving objects in Eden and movie vor to another Suivivior at one time, and finally clear the Eden and the occupied movie vor space.

Copy Algorithm

3. Mark-Compact)

The replication collection algorithm performs a large number of replication operations when the object survival rate is high, which reduces the efficiency. More importantly, if you do not want to waste 50% of the space, you need to allocate additional space to ensure that all objects in the half-zone memory are 100% alive, therefore, this algorithm cannot be directly used in the old age.

Therefore, people have proposed another Mark-Compact algorithm. Because the object lifecycle in the old age is long, some people have proposed the Mark-sort algorithm, the marking process is the same as the "mark-Cleanup" process, but the surviving objects are organized while the Dead objects are cleared, which can reduce the size of the fragment space.

Tag-Sorting Algorithm

Iv. Generation-based collection

Currently, commercial Virtual Machine garbage collection uses the Generational Collecting algorithm, which has no new ideas, the memory is divided into several parts based on the different lifetime of the object. Generally, Java heap is divided into the new generation and the old generation, so that the most appropriate collection algorithm can be used according to the characteristics of each generation. In the new generation, a large number of objects are found to die during each garbage collection, and only a small number of objects survive. The replication algorithm is used to complete collection with only a small copy cost. In the old age, because objects have a high survival rate and long cycle, they are recycled using the "tag-sort" or "tag-clear" algorithms.

The preceding introduction based on common garbage collection algorithms in JVM is a small part of the Content shared by everyone. I hope to give you a reference, and I hope you can provide more support for the customer's house.

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.