Garbage collector and Memory allocation Strategy (ii) garbage collection algorithm

Source: Internet
Author: User

Garbage collector and Memory allocation policy (ii)--garbage collection algorithm JAVAJVM garbage collector

Simple understanding of the idea of algorithms

1. Mark-Sweep algorithm

Mark-sweep algorithm is divided into two stages: Mark and clear the objects that need to be recycled (see the accessibility analysis in the previous section to find the surviving objects), and then collect all the tagged objects uniformly after the tag is complete.
   

Disadvantages:
1. Two processes are not efficient to mark and clear
2. Spatial problems, when a tag is cleared, there is a large amount of discontinuous memory fragmentation, and too much space fragmentation can cause a large object to be allocated later in the program to run, and you cannot find enough contiguous memory to trigger another garbage collection action in advance.

2. Copy algorithm

Copy algorithm: Divides available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, copy the surviving object to the other piece, and then clean up the used memory space once.

   

Advantage: Each time is the entire half of the memory collection, memory allocation without regard to the complexity of memory fragmentation, just move the heap top pointer, in order to allocate memory. Easy to implement and efficient to run.
Cons: Shrinking the memory to half the original price is a bit high.

Application:
In the Cenozoic 98% of the objects are "dying", you can not press 1:1 to allocate memory space. Divide the memory into a larger Eden space and two smaller survivor spaces, each using Eden and one of the survivor spaces. When recycled, the objects that are still alive in Eden and survivor space are copied once to another survivor space, and finally the Eden and survivor space that you just used is cleared.

In the hotspot virtual machine Eden Space and survivor space default this scale is 8:1, that is, each new generation of free space is 90%. When you want to perform a garbage cleanup to copy an object to another unused survivor space but survivor space is not enough, additional memory is required (this refers to the old age) for allocation guarantees.

3. Marking-Sorting algorithm

Tag-Grooming algorithm: The tagging process is the same as the mark-clear process (see the accessibility analysis in the previous section to find the surviving object), except that in the finishing phase, all surviving objects are moved to a segment, and then the memory outside the end boundary is cleaned up directly.

   

Application: Suitable for the old age (because of the high survival rate of the old age object, it does not "waste" space.) )

4, Generational collection algorithm

Generational collection algorithm: The combination of copy algorithm and marker-collation algorithm. The current commercial virtual machines use a "generational collection" algorithm that divides memory into chunks based on the different lifetime of the object. The Java heap is generally divided into the new generation and the old age.

In the Cenozoic, each garbage collection will be killed by a large number of objects, only a small number of surviving, so the "copy" algorithm. Collection can be done with a small amount of replication costs for the surviving objects.
In the old age, because the object has a high survival rate and there is no additional space to guarantee it, it must be recycled using the "mark-clear" or "mark-sweep" algorithm.

Garbage collector and Memory allocation Strategy (ii) 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.