Garbage collection and several common garbage collection algorithms

Source: Internet
Author: User

Objective:

Think first about the three things you need to do with garbage collection (garbage COLLECTION,GC)

1) What memory needs to be recycled?

2) When is it recycled?

3) How to recycle?

The previous blog mentions the various parts of the Java Memory Runtime area, where the program counter, virtual machine stack, local method stack 3 regions will be born with threads, and the stack frames in the stack execute the stack and stack operations methodically with the method. The amount of memory allocated in each stack frame is basically known when the class structure is determined, so the memory allocation recycling in these areas is deterministic, and in these areas there is no need to think too much about recycling because the memory is recycled at the end of the method or at the end of the thread. The Java heap and the method area are different, the implementation class in an interface may require different memory, multiple branches in a method may require different memory, we can only know when the program is running to create those objects, this part of the memory allocation and recycling is dynamic, The garbage collector is concerned about this part of the memory.

One, the object is dead

1. The so-called object is dead, in fact, the garbage collector before the heap recycling needs to determine which of these objects are "alive", which has "died"

2. Judgment algorithm

1) Reference counting method (Reference counting): Many textbooks to determine whether the survival of the object is the algorithm, but in the mainstream Java Virtual machine does not choose this algorithm to manage memory, the following simple introduction to this algorithm, in fact, is to add a reference counter to the object, Whenever a place refers to it, the counter increases by 1, and when the reference fails, the counter is reduced by 1, and any object with a counter 0 at any time is impossible to use again.

2) Accessibility analytical algorithm (reachability analysis): In the implementation of the mainstream commercial programming language, the accessibility analysis method is used to determine whether the object survives. The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain (Reference Chain), when an object to the GC Roots no reference chain connected, It proves that this object is not available.

    

In the Java language, objects that can be used as GC roots include:

Objects referenced in the virtual machine stack (local variable table in the stack frame)

Objects referenced by constants in the method area

Object referenced by class static property in method area

The object referenced by JNI (that is, the general native method) in the local method stack

3. References

In fact, regardless of the algorithm to determine whether the object is dead, judgment is related to "reference"

In Java, references can be divided into strong references, soft references, weak references, four of virtual references, and the 4 reference strength is reduced once

1) Strong references: a reference that is common among programs, such as "Object obj = new Object ()", as long as a strong reference exists, the garbage collector never reclaims the referenced object

2) Soft reference: Used to describe some useful but not necessary objects. For objects that are associated with soft references, these objects are then listed in the collection scope for a second collection before the system will have a memory overflow exception. If this collection does not have enough memory, a memory overflow exception will be thrown

3) Weak references: It is also used to describe non-essential objects, but its strength is weaker than soft references, and objects associated with weak references can only survive until the next garbage collection occurs. When the garbage collector is working, the object associated with the weak reference is reclaimed regardless of whether the current memory is sufficient

4) Virtual reference: Also become a ghost reference or welcome reference, it is the weakest of a reference relationship. Whether an object has a virtual reference exists, does not affect its lifetime at all, and cannot obtain an object instance through a virtual reference

Second, garbage collection algorithm

1. Tag-Clear algorithm

The most basic collection algorithm "Mark-Clear" (mark-sweep) algorithm, the algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the mark is complete, the unified collection of all tagged objects, it is said that it is the most basic collection algorithm, is because the subsequent collection algorithms are based on this idea and to improve their shortcomings. Its main deficiencies are two:

One is the efficiency problem, the mark and clear efficiency is not high, two is the space problem, the mark clears will produce a large number of discontinuous memory fragments, too much space debris may cause the program in the run process need to allocate large objects, can not find enough contiguous memory and have to trigger another garbage collection action in advance

The execution process is as follows:

    

2. Copying algorithms

To solve the efficiency problem, a collection algorithm called "Replication" (Copying) appeared, and he divided the available memory by capacity into two blocks of equal size, using only one piece at a time. When this block of memory is used up, the objects that are still alive are copied to the other, and then the used memory space is cleaned out once. This makes every time the entire half of the memory collection, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer, in order to allocate memory, easy to implement, efficient operation. Just the cost of this algorithm is to reduce the memory for the original half, it is a little too high.

    

3. Labeling-Sorting algorithm

The replication collection algorithm will perform more replication operations when the object has a higher survival rate, and the efficiency is reduced. What's even more critical is that if you don't want to waste 50% of your space, you'll need to use extra space for the allocation guarantee (Handle promotion If you don't have enough space to rely on other memory) to cope with the extreme situation where all objects in the used memory are 100% alive.

For the mark-and-organize algorithm, the tagging process is still the same as the mark-purge 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, the "mark-and-organize" algorithm is as follows:

    

4. Generation of collection algorithms

The current garbage collection of commercial virtual machines is based on the "generational collection" (generational Collection) algorithm, which does not have any new ideas, but divides the memory into chunks based on the different life cycles of the objects. Generally, the heap is divided into the new generation and the old age, so that it can be based on the characteristics of each age to adopt the most appropriate collection algorithm. In the new generation, each garbage collection found that a large number of objects died, only a small number of survival, then the use of replication algorithm, only a small number of surviving objects to pay the cost of replication can be completed collection. In the old age, because the object has a high survival rate and no additional space to guarantee it, it must be recycled using a "mark-clean" or "mark-sweep" algorithm

Three, memory allocation and recovery strategy

Object memory allocation to the general direction, is allocated on the heap, the object is mainly distributed in the new generation of Eden area (a generational layout, the new generation of memory into a larger Eden space and two smaller survivor space, here does not elaborate, understanding is good), If the local thread allocation buffer Tlab (thread local Allocation buffer) is started, it will be assigned on the Tlab by thread precedence. In a few cases it may also be assigned directly in the old age, the allocation rules are not completely fixed, the details depend on which garbage collector combination is currently in use, and the memory-related parameter settings in the virtual machine

1. Object Precedence in Eden Assignment

In most cases, objects are allocated in the Cenozoic Eden region. When the Eden Zone does not have enough space to allocate, the virtual machine initiates a new generation of garbage collection (Minor GC: A new generation of garbage collector actions, because most Java objects have an out-of-date feature, so the Minor GC is very frequent and generally recycles faster. And here, incidentally, the old age garbage collection Major GC: Often accompanied at least once minor gc,major GC speed is generally 10 times times slower than minor GC)

2. Large objects directly into the old age

A large object is a Java object that requires a large amount of contiguous memory space, and the most typical large object is the long string and array. Large objects are bad news for the memory allocation of virtual machines, and often large objects tend to cause memory to have plenty of space to trigger garbage collection ahead of time to get enough contiguous space to accommodate them.

3. Long-term survival of the target will enter the old age

Now that the Java virtual machine uses the idea of generational collection to manage memory, memory recycling must be able to identify which objects should be placed in the new generation and which should be in the old age. To do this, the virtual machine defines an object age counter for each object. If the object survives after Eden was born and is still alive after the first minor GC and can be accommodated by survivor, it will be moved to survivor space, and the object age is increased by 1 years, and when its age increases to a certain extent (by default 15), it will be promoted to the old age.

4. Dynamic Object Age Determination

In order to better adapt to the memory situation of different programs, virtual machines do not always require the age of the object must reach the maximum age to promote the old age, if the same age in Survivor space the sum of all object size is greater than half of survivor space, Objects older than or equal to that age can enter the old age without waiting for the maximum age

5. Space Allocation Guarantee

Before the minor GC occurs, the virtual opportunity checks whether the largest available contiguous space in the old age is greater than the total space of all new generation objects, and if this condition is true, then the minor GC can ensure that it is safe. If you do not establish a virtual opportunity, see if the Handlepromotionfailure setting value allows the warranty to fail. If allowed, then continue to check whether the largest available continuous space in the old age is greater than the average size of the previous promotion to the old age object, if greater than, will try to do a minor GC, although this time the minor GC is risky, if it is less than, or does not allow the guarantee to fail, So this is going to be an old age. Garbage collection

Explain what the risk is: The new generation uses the Replication collection algorithm, but for memory utilization, only one of the survivor spaces is used as a rotational backup, so when a large number of objects survive after the minor GC (most extreme is that the objects in the Cenozoic are alive after the memory is reclaimed) , we need the old age to carry on the allotment guarantee, the survivor cannot accommodate the object to enter the old age directly

Add:

Reference: Drill down into Java virtual machines

The garbage collection part and the memory allocation part did a simple collation, for everyone to quickly understand this part of the knowledge point, want to learn more can go to see the book

Garbage collection and several common garbage collection algorithms

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.