Basic garbage collection Algorithm

Source: Internet
Author: User

Original article address: iteye basic garbage collection Algorithm

Garbage collection algorithms can be divided from different perspectives:

Score by basic recovery policy
Reference counting ):

The old recycling algorithm. The principle is that this object has a reference, that is, adding a count. deleting a reference reduces the count. During garbage collection, only objects with zero collection count are used. The most critical issue of this algorithm is that it cannot handle circular references.

 

Mark-sweep ):

 

This algorithm is executed in two phases. In the first stage, all referenced objects are marked from the reference root node. In the second stage, the whole heap is traversed to clear unmarked objects. This algorithm suspends the entire application and generates memory fragments.

 

Copy ):

This algorithm divides the memory space into two equal regions and uses only one of them at a time. During garbage collection, traverse the current region and copy the objects in use to another region. The secondary algorithm only processes the objects in use at a time, so the replication cost is relatively small. At the same time, the corresponding memory can be organized after the replication, without the "Fragmentation" problem. Of course, the disadvantage of this algorithm is also obvious, that is, it requires two times of memory space.

 

Mark-compact ):

This algorithm combines the advantages of "tag-clear" and "copy" algorithms. It is also divided into two phases. In the first phase, all referenced objects are marked from the root node. In the second stage, the whole heap is traversed to clear unlabeled objects and compress the surviving objects to one of the heap, discharge in sequence. This algorithm avoids the "tag-clear" fragmentation problem, and also avoids the space problem of the "copy" algorithm.

Partitions
Incremental collecting ):

Real-time garbage collection algorithm: Garbage collection is performed simultaneously by applications. I don't know why the Collector in jdk5.0 does not use this algorithm.

 

Generational collecting ):

Based on the garbage collection algorithm obtained after object lifecycle analysis. Objects are divided into young, old, and persistent generations. Different algorithms (one of the above methods) are used to reclaim objects in different lifecycles. The current garbage collector (beginning with j2se1.2) uses this algorithm.

 

By system thread
Serial collection:

Serial collection uses a single thread to process all garbage collection tasks, because multi-thread interaction is not required, which is easy to implement and highly efficient. However, its limitations are also obvious, that is, the advantage of being unable to use multi-processor, so this collection is suitable for single-processor machines. Of course, this collector can also be used on a multi-processor machine with a small amount of data (about MB.

 

Parallel collection:

Parallel collection uses multiple threads to process garbage collection, which is fast and efficient. In theory, the more CPUs, the more advantageous the parallel collector is.

 

Concurrent collection:

Compared with serial collection and parallel collection, the first two must suspend the entire runtime environment while the garbage collection program is running. Therefore, the system will suspend garbage collection obviously, and the pause time will be longer because of the larger the heap.

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.