JVM Garbage collector 2 (garbage collection Algorithm)

Source: Internet
Author: User

  Based on the "in-depth understanding of Java Virtual Machines", this book summarizes

  I. About several concepts: (tag garbage algorithm, garbage collection algorithm, garbage collector)

There are two ways to find JVM garbage: The reference counting method/accessibility algorithm. This preparation, after marking the garbage, the recovery algorithm, where the algorithm is just garbage collection idea. There will be a variety of garbage collectors, the garbage collector here is the idea of using the garbage mobile algorithm, can be said to be concrete implementation.

Here's a little extra talk about these three concepts:
Garbage tagging algorithm: A method of marking garbage

Garbage collection algorithm: A collection of ideas for use by the garbage collector. May be used in younger generations, or in the old age (of course, the old and young generations of the algorithm are basically fixed)

Garbage collector: The implementation of garbage collection algorithm, the real recycling of garbage collectors. A garbage collector can be used in different areas and can be used in conjunction with unused garbage collectors to reclaim tagged garbage.

  Two or three kinds of garbage collection algorithms

1. Mark-Clear:   Mark Garbage --and clean up trash directly

This algorithm has two drawbacks: 1, efficiency: the process of marking and purging is time consuming. 2, the space problem: Because the memory allocation is a piece of, directly clear the marked garbage, does not carry on the space arrangement, will produce many garbage fragments, may all the free space is large, but does not have the continuous large space, causes the large object to be unable to store.

  2, Replication algorithm: The replication algorithm obviously requires at least two areas, garbage collection when the available objects are copied sequentially to an empty area, the original area is cleared directly. This solves the problem of space debris. Specific ideas are as follows:

(1) The memory is divided into two equal areas, the allocation of memory only use a single area, when the area is full, when the GC is required, mark the area of the available objects copied to another area, and cleared. However, this approach leads to low memory utilization, and only half of the memory can be used within a single time.

(2) Divide the memory into three areas: the larger Eden area, two smaller survivor1 and 2 zones, allocate memory only to Eden, and when GC, copy the Eden available objects to the idle Survivor1, clearing Eden. Continue to allocate memory, assign to Eden, and when it is necessary to re-GC, copy the available objects in Eden and Survivor1 into the Survivor2, so loop.

In general, the second method is now used, the space utilization of this method is improved, the proportion of three space is: 8-1-1, utilization at 90%. However, since replication can only be replicated to 10 of the region, so the need for in-memory object garbage accounted for more. So this method, generally used in the new generation, these objects are mostly created after the need to recycle, of course, if there is a situation where survivor is not enough, there is a space allocation guarantee mechanism, which is usually allocated to the old age area.

  3, Mark-finishing: Mark Garbage ---> available Object collation

(1) Marker-collation algorithm, compared with the mark-clearing algorithm, optimizes the space fragmentation a bit and moves the available objects forward.

(2) compared to the replication algorithm, the replication algorithm is more suitable for the region of the object survival rate is not high, for the new generation is more suitable. But for the old age, the target survival rate was high. At this time, the algorithm with the marker collation will be more appropriate.

  Iii. about the young generation and the old age

    General memory will be divided into the younger generation and the old age. What kind of objects will appear in the young generation? What kind of objects will appear in the old age? The general object of the young generation is given priority to the younger generation. The situation of the old age has the following kinds:

1, Large objects: The JVM can set the value, if the object is too large, will be directly into the old age, into the array ah what.

2, the young generation transferred over: the object assigned to the younger generation will have an age attribute, each time the GC, if the object is still alive, will give the age plus 1, if the default is greater than 15/or the same age than half of the memory can not reach the age of the set, it will be transferred to the old age.

It can be seen that the relatively high survival of the object, half will be placed in the old age, so the object above the algorithm:

Old age: Mark-clear, Mark-organize

Young generation: Replication Algorithms

        

    

    

  

JVM Garbage Collection 2 (garbage collected 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.