JVM garbage collection algorithm and jvm garbage collection

Source: Internet
Author: User

JVM garbage collection algorithm and jvm garbage collection

1. Mark-cleanup algorithm: the most basic garbage collection algorithm. Just like its name, the algorithm consists of two steps: Marking and cleanup: first, marking the objects to be cleansed, after marking is completed, all marked objects are recycled in a unified manner. The disadvantage of this algorithm is efficiency, low efficiency in the marking and clearing processes, and space, after the mark is cleared

Generates a large number of unrelated memory fragments, too many space fragments may cause the program to fail to find enough continuous memory when a large object needs to be allocated during future running, and it has to trigger another garbage collection action in advance.

2. The replication algorithm, in order to solve the efficiency problem of the tag clearing algorithm, is a replication algorithm that divides the available memory into two equal-size blocks by capacity, each time you only use one of them, when one of them is used up, the memory-alive objects will be copied to the other, and the memory space that has been used will be cleared once,

In this way, each time a memory block is recycled, you do not need to consider memory fragmentation and other issues when allocating the memory. You only need to move the heap top pointer, which is easy to implement, efficient operation. the cost of this algorithm is to reduce the memory to half of the original one, which may not be a bit high. The execution process of the copy algorithm is as follows:

 

 

3. Mark Sorting Algorithm copy collection algorithms perform many copy operations when the object survival rate is high, and the efficiency will be affected. More importantly, if you do not want to waste 50% of the space, the extra space is needed to allocate a guarantee to deal with the extreme situation where all objects in the memory used are 100% alive. Therefore, this algorithm cannot be directly selected in the old age.

According to the characteristics of the old age, someone proposed another algorithm for marking-sorting, which is the same as the algorithm for marking-clearing, however, the subsequent steps do not directly clean up recyclable objects, but move all surviving objects to one end, and then directly clear the memory outside the end boundary, the process of "tag-Sort algorithms" is as follows:

The 4-generation collection algorithm divides the memory into several parts based on different lifecycles of objects. Generally, Java heap is divided into the new generation and old generation, in this way, you can use the most appropriate collection algorithm based on the characteristics of each age. in the new generation, a large number of objects die in each garbage collection process. As long as a small number of objects survive, the replication algorithm is used.

The collection can be completed at the replication cost of a small number of surviving objects. in the old age, because the object has a high survival rate and there is no extra space to allocate a guarantee for it, you must use the "mark-clear" or "mark-sort" algorithm for recovery.

5. The main garbage collectors are Serial collectors, ParNew collectors, Parallel Scavenge collectors, Serial Old collectors, Parallel Old collectors, CMS collectors, and G1 collectors.

 

References: deep understanding of Java virtual machines

 

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.