In-depth understanding of Java Virtual Machines Note 02-Garbage collection algorithm

Source: Internet
Author: User

1. Tag-Clear algorithm

All objects that need to be reclaimed are marked first, and all tagged objects are collected uniformly after the mark is complete. It is the most basic collection algorithm. Other collection algorithms are based on their ideas to improve their shortcomings.

Cons: 1 The efficiency of marking and clearing two phases is not high; 2) a large number of discontinuous memory fragments are produced when cleared

2. Copying algorithms

Divide the available memory into two blocks of equal size, using only one piece at a time. When one piece of memory is exhausted, copy the surviving object to another, and then empty the used block.

Disadvantage: Memory can be used only half of the allocation, the cost is too high.

This algorithm is mainly used in the Cenozoic. Most of the new generation of objects are dying, so it does not require 1:1 to divide, but is divided into a larger Eden area and two smaller survivor areas, each using Eden and one of the survivor. When recycled, the objects that are still alive in Eden and survivor are copied one at a time into another survivor area, finally clearing up Eden and the survivor space just used. The default Eden and survivor size ratio for the hotspot virtual machine is 8:1.

3. Labeling-Sorting algorithm

The tagging process is the same as the mark-sweep algorithm, but the next step is not to clean up the recyclable objects directly, but rather to have all the surviving objects move toward one end and then directly clean out the memory outside the end boundary.

4. Generation of collection algorithms

According to the life cycle of the object, the Java heap is generally divided into the new generation and the old age. The new generation adopts the replication algorithm, and the old age adopts the "mark-clean" or "mark-tidy" algorithm.

In-depth understanding of Java Virtual Machines Note 02-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.