Java garbage collection mechanism

Source: Internet
Author: User
Tags compact

Memory distribution in Java

In the JVM, memory is organized in terms of generational.

Among them, heap memory is divided into young generation and old generation, non-heap memory is mainly permanent area, mainly used for storing some kinds of metadata, constant pool and other information. And the younger generation is divided into two kinds, one is Eden area, the other is two size equivalent survivor area. The reason that Java memory is organized on a generational basis is based on a "weak hypothesis"-most of the objects die in their youth. At the same time, the memory is organized according to generational, so that we can use different garbage collection algorithms on different generations, which makes the whole memory garbage collection more effective.

Garbage collection for young generations

The garbage collection algorithm used in the young generation is the "mark-copy" algorithm, which differs from any of the basic garbage collection algorithms we have previously known, but the mark algorithm is the same, based on the root object to find all the available objects, see the mark-sweep algorithm in the mark step. For the copy algorithm, it is simply a simple copy of an object that matches a certain age from one generation to another. The specific recycling process is as follows:

First, the memory allocations for the new objects are first made in the Eden Zone, and when the Eden area is not enough space to allocate new objects, it triggers a garbage collection on the young generation (which occurs on the Eden and Survivor memory areas), which we call "minor garbage Collection ". At the same time, each object has an" age ", which actually refers to the number of minor GC that the object has experienced. As shown in 1, when an object is just assigned to the Eden region, the age of the object is "0", and when the minor GC is triggered, all surviving objects (still up to the object) are copied to one of the survivor areas, and the age is increased to "1". and clears the non-accessible objects in the entire Eden memory area.

When the second minor GC is triggered (2), the JVM will use the mark algorithm to find all the objects that survived in the Eden memory area and the Survivor1 memory area and copy them to the new Survivor2 memory area ( This is why it takes two survivor regions of the same size, and the age of the object plus 1. Finally, clear all non-accessible objects in the Eden memory area and the Survivor1 memory area.

When the age of the object is large enough (this age can be specified by the JVM parameter, which is assumed to be 2), when the minor GC occurs again, it is upgraded from the Survivor memory area to the older generation, as shown in 3.

In fact, even if the object is not old enough, but there is not enough space in the Survivor memory area to accommodate objects upgraded from Eden, some objects will be upgraded directly to the tenured memory area.

Garbage collection for older generations

When the minor GC occurs, another object is upgraded from the Survivor Zone to the tenured zone, but there is no space in the tenured area to accommodate the new object, so this time it triggers garbage collection on older generations, which we call "major garbage Collection ".

The garbage collection algorithm chosen on older generations depends on what garbage collector is used on the JVM. There are two types of garbage collector: Parallel scavenge (PS) and concurrent Mark Sweep (CMS). The two garbage collector's differences are more in the old generation of garbage collection process, the young generation of garbage collection process in the two garbage collector is basically the same.

As its name indicates, the Parallel scavenge garbage collector uses multiple threads for garbage collection when it performs garbage collection, which can increase the efficiency of garbage collection. While the concurrent Mark sweep garbage collector is garbage collected, the application can run concurrently.

Parallel Scavenge

The garbage collection algorithm used in the old generation of PS garbage collector can be regarded as a combination of the tag-purge algorithm and the tag-compression algorithm.

First, the PS garbage collector uses the mark-and-sweep algorithm on older generations to reclaim space occupied by non-accessible objects, but we know that one drawback of the markup cleanup algorithm is that it causes memory fragmentation problems. It is then possible to trigger a continuous major GC. Assuming that there is currently 10M of memory fragmentation, but the largest memory fragment can only accommodate 2M of objects, this time if there is a 3M object from the survivor zone to the tenured region, the tenured region also has no way to store this 3M object. The result is a constant triggering of the major GC until the out of Memory. Therefore, after the PS garbage collector clears non-accessible objects, it also makes a compact to eliminate memory fragmentation.

Concurrent Mark Sweep

Compared to the PS garbage collector, the CMS garbage collector successfully reduces the time to suspend applications when garbage collection occurs, because the application can run in parallel when the CMS is garbage collected. Let's take a look at how it's done.

As you can see from its name, the garbage collection algorithm used by the CMS garbage collector in older generations is a mark-and-sweep algorithm. However, it is slightly different from the standard tag-purge algorithm. It consists of four phases:

    1. Initial Mark Phase-this phase is stop-the-world, it pauses the application, but at this stage it does not mark all the objects that are available in the tenured area. It only starts from the root object, and the first child node that is marked to the root object stops. Then restore the application to run. Therefore, the time to pause the application is very short.
    2. Concurrent Mark Stage-During this phase, the CMS garbage collector restarts the initial object in the tag tenured area with the node marked by the Mark stage as the root object. Of course, there is no need to pause the application during this phase. That's why it's called "Concurrent Mark." This also creates the problem that because the CMS garbage collector and the application are running concurrently, the Concurrent mark phase does not guarantee that the accessible objects in the tenured area are marked-the application is always allocating new objects.
    3. Remark stage-due to the concurrent mark phase it does not guarantee that the accessible objects in the tenured area are marked, so we need to pause the application again to ensure that all the accessible objects are marked. To speed up, the multi-thread is also used to mark objects at the same time.
    4. Concurrent Sweep Phase-Finally, the execution of the application is resumed, and the CMS executes the sweep to clear up the memory space occupied by all non-accessible objects.

You can see the difference between the PS and CMS garbage collectors:

The black arrows represent the running of the application, and the green arrows represent the operation of the CMS garbage collector. A line represents a single thread, and multiple lines represent multithreading.

Therefore, compared to the PS garbage collector, the CMS garbage collector successfully reduces the transient time of the application.

Garbage first (G1) garbage collector

Unfortunately, though, the CMS garbage collector reduces the time it takes to pause the application, but because it has no compact phase, it still has a memory fragmentation problem. Thus, in order to remove the memory fragmentation problem while preserving the benefits of the CMS garbage collector's low pause time, JAVA7 released a new garbage collector-G1 garbage collector. It will gradually replace the CMS garbage collector in the future.

There are several differences between the G1 garbage collector and the CMS garbage collector. First, the biggest difference is that the way memory is organized has changed. Memory areas such as Eden,survivor and tenured are no longer contiguous, but instead become regions of the same size-each region ranges from 1M to 32M.

A region may belong to the Eden,survivor or tenured memory area. The E in the figure indicates that the region belongs to the Eden Memory area, S represents the Survivor memory area, and T indicates that it belongs to the tenured memory area. The blank representation of unused memory space in the figure. The G1 garbage collector also adds a new area of memory called the H block in the Humongous memory area. This memory area is primarily used to store large objects-objects that are larger than 50% of the size of a region.

In the G1 garbage collector, the garbage collection process for young generations is similar to the PS garbage collector and CMS garbage collector, where the new objects are allocated in the Eden region, and when all Eden region sizes exceed a certain value, the minor GC is triggered, and the Eden is recycled Non-accessible objects on region and survivor region, while upgrading surviving objects to the corresponding survivor region and tenured region. The upgrade of an object from survivor region to tenured region remains dependent on the age of the object.

For the old generation of garbage collection, the G1 garbage collector is also divided into 4 stages, basically the same as the CMS garbage collector, but slightly different:

    1. Initial Mark Stage-like the Initial Mark stage of the CMS garbage collector, G1 also pauses the execution of the application, which marks all the objects that can be reached from the root object in the first-level child node of the root object. But the initial mark phase of G1 's garbage collector took place with the minor GC. That is, in G1, you do not have to pause the execution of the application as you would in the CMS to run the initial mark phase, but instead G1 initial mark on the old generation when the minor GC is triggered.
    2. Concurrent Mark Stage-at this stage G1 does the same thing as a CMS. But G1 also did one more thing, that is, if in the concurrent mark stage, discovered which tenured region the survival of the object is very small or basically no object to survive, then G1 will be at this stage to reclaim it, instead of waiting for the later clean up stage. This is also the origin of garbage first name. Meanwhile, at this stage, G1 calculates the survival rate of each region's object, facilitating the use of the clean up stage behind it.
    3. Remark stage-At this stage G1 does the same thing as a CMS, but with different algorithms, G1 uses an algorithm called SATB (snapshot-at-the-begining) that can mark objects faster in the remark phase.
    4. Clean up/copy Stage-in G1, there is no corresponding sweep stage in the CMS. Instead it has a clean up/copy phase, in which G1 picks up the region where the object's survival rate is low, which is also happening with the minor GC, as shown in:

As you can see, the initial mark phase and the clean up/copy phase are all simultaneous with the minor GC, which increases the efficiency of garbage collection compared to the less time it takes to pause the application in CMS,G1.

Excerpt from: http://www.jianshu.com/p/778dd3848196

Java garbage collection mechanism

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.