(vi) Garbage collection algorithm

Source: Internet
Author: User

One, mark-clear algorithm 1) Mark-Clear algorithm

The most basic collection algorithm is the "mark-clear" (mark-sweep) algorithm, like its name, 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 completed, the unified collection of all tagged objects, Its tagging process has been basically introduced in the previous section about object tag determination. The reason is that it is the most basic collection algorithm, because the subsequent collection algorithms are based on this idea and improve their shortcomings. Its main shortcomings are two: one is the efficiency problem, the labeling and removal process is not efficient, and the other is a space problem, the mark after the purge will produce a large number of discontinuous memory fragmentation, too much space debris may cause, When the program needs to allocate large objects in the future, it cannot find enough contiguous memory and has to trigger another garbage collection action in advance. Tag-Clears the execution process of the algorithm

Second, the replication algorithm

To solve the efficiency problem, a collection algorithm called "Replication" (Copying) appears, dividing the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once. This makes each one of the pieces of memory recycling, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer, in order to allocate memory, simple implementation, efficient operation. But the cost of this algorithm is to reduce the memory to half of the original, it is a little too high. The execution process of the replication algorithm

Three, the marking finishing algorithm 3) Labeling-Sorting algorithm

The replication collection algorithm performs more replication operations when the object has a higher survival rate and becomes less efficient. More crucially, if you do not want to waste 50% of space, you need to have additional space to allocate security, in order to deal with all the objects in the memory used in 100% survival extreme situation, so in the old age generally can not directly select this algorithm.
According to the characteristics of the old age, someone proposed another "mark-and-sweep" (mark-compact) algorithm, the marking process is still the same as the "tag-purge" algorithm, but the next step is not directly to the recyclable objects to clean up, but to let all the surviving objects moved to one end, Then directly clean out the memory outside the end boundary, the "mark-and-sweep" algorithm

Iv. Collection Algorithm of generation 4) Generational collection algorithm

The current garbage collection of commercial virtual machines uses the "generational collection" (generational Collection) algorithm, which does not have any new ideas, but divides the memory into several blocks based on the different life cycles of the objects. The Java heap is generally divided into the new generation and the old age, so that according to the characteristics of each era to adopt the most appropriate collection algorithm. In the Cenozoic, every garbage collection is found to have a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects of the replication cost 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 the "mark-clean" or "mark-sweep" algorithm

V. Comprehensive

The JVM memory model is divided into two chunks, one is New Generationand the other is an old Generation. In new generation, there is a space called Eden, which is mainly used for storing new objects, and two survivor Spaces (From,to), which are used to store objects that survive each garbage collection. In old Generation, a memory object with a long life cycle in the application, and a Permanent Generation, are used primarily to put the JVM's own reflective objects, such as class objects and method objects.
1) in the new Generation block, garbage collection is generally used in the replication algorithm, fast. At each GC, the surviving objects are first copied from Eden to a survivor space, and when the survivor space is full, the remaining live objects are copied directly to the old generation. Therefore, after each GC, the Eden memory block is emptied

2) in the old generation block, garbage collection is generally marked with an algorithm that is slower, but reduces memory requirements.

Garbage collection sub-level, Level 0 for all (full) garbage collection, will reclaim the old section of garbage, Level 1 or above is a partial garbage collection, will only reclaim new garbage, memory overflow usually occurs after the old or perm segment garbage collection, still no memory space to accommodate the new Java object situation.

The out of memory only occurs when the JVM does not have enough memory after the old and perm generation have been reclaimed.
When a new object is generated, the memory request process is as follows:
A. JVM tries to initialize a chunk of memory in Eden for related Java objects
B. When the Eden space is sufficient, the memory request ends. Otherwise to the next
C. The JVM tries to free all inactive objects in Eden (this is a garbage collection of 1 or more advanced), and attempts to put some of the active objects in Eden into the survivor area if Eden space is still insufficient to fit into the new object after release
D. The survivor area is used as an intermediate swap area for Eden and old, and when the old area is sufficiently large, the objects in the Survivor area will be moved to the old area, otherwise they will remain in the survivor area.
E. When there is not enough space in the old area, the JVM performs a full garbage collection in the old area (level 0)
F. After a complete garbage collection, "Out of memory error" occurs if the survivor and old areas still cannot hold portions of objects copied from Eden, causing the JVM to be unable to create an area for the new object in the Eden area

Causes of full GC
New a lot of objects, not immediately in the active release of->eden memory is not enough, and constantly moved the object to old->old full->full GC

Summary: The above describes how the Java Virtual Machine Management objects, we also see the above is mainly the collection of algorithms and heap space from the new division, so that the purpose is to garbage collection efficient implementation, but it seems, if the object to the system to manage, the process efficiency of the system will certainly have an impact, But this is a bit better, that is, do not need manual management, to bring convenience to the program ape.

(vi) 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.