Java virtual machines and garbage collection mechanisms (ii)

Source: Internet
Author: User
Tags garbage collection
It's been more than half a month since the last article, and there's a continual study of the JVM's features, and this article will continue to delve into more of the JVM's content

Preface

The last article talked about the Java compiler and load principles, such as Java compiled into a class file after the runtime loading mode, as well as the memory structure of the JVM, and finally talked about the JVM heap and method area in the generational mechanism. Java is called Java because of its unique and perfect compatibility of a compile-run features, Python and C # although also emulated Java but because its platform does not have Java compatibility good so it is difficult to promote, such as Python 2. The X and 3.X versions are a large cross-domain, but not fully compatible, and their bytecode-transcoding efficiency is not as high as Java, and C # is not to mention, almost exclusively Windows exclusive, not very suitable for Linux, but also indirectly doomed the two languages of the ability to play a limit


Why do you want to be generational?

First of all, I explained that it is not because the generation of Java to GC garbage collection, no generational mechanism as long as you can determine which objects need to be recycled, the JVM can be recycled, then why should be generational. There's a reason for it. Obviously, for efficiency, generational can make GC more efficient.

You can try to think that if all the objects are in memory without being divided, so every time the JVM reclaims objects to be scanned from the back of memory, how inefficient, and many objects actually live not long, executed dead, then the frequent large-scale scanning of memory must make the JVM inefficient, So generational benefits efficient garbage collection.


How to divide the generation.

The JVM mainly divides objects into three generations (you can read my previous article: Java Virtual machines and garbage collection mechanism (i)): Young generation, old age, lasting generation

Young generation:

All newly generated objects are first placed in the younger generation. Most of the younger generation are objects with shorter life cycles. There are three districts in the young generation. An Eden area, two survivor districts. Most objects are generated in the Eden area. When the Eden Zone is full and triggers garbage collection, the surviving object will be copied to the Survivor area, and when the survivor is full, the surviving object of the area will be copied to another survivor area, and when the survivor is full, Objects copied from the first survivor area and still alive at this time will be replicated in the old age area (tenured). Note that the two areas of survivor are symmetrical and have no precedence, so there may be simultaneous objects in the same zone that are replicated from Eden, and those that were copied from the previous survivor, and those that were copied to the old age only came from the first survivor. Moreover, one of the survivor areas is always empty. At the same time, the Survivor area can be configured as multiple (more than two) depending on the program, which can increase the presence of objects in the younger generation and reduce the likelihood of being placed in the older generation.


Older generation:

In the younger generation, those who survived after N garbage collection will be placed in the older generation. Therefore, it can be considered that the older generation of the storage of some of the longer life cycle objects.


Persistent generation (permanent generation):

For storing static files, constant pools, now Java classes, methods, and so on. Basically, garbage collection does not participate, does not affect garbage collection (Jdk7 began to abolish this classification, in fact, can be said from the beginning of Jdk8 no permanent generation, JDK8 constant pool into the heap area to participate in garbage collection)


how to recycle.

The principle of garbage collection is divided into two steps, the first step is to filter out the objects that need to be recycled, and the second to recycle them. To recycle this object, you first have to be sure that the object is not needed in front of the mass of objects, one traversal is very inefficient, so we need algorithms to solve this problem

Judging Garbage objects:1, reference counting method 2, accessibility analysis

traditional garbage collection algorithm:1, Mark elimination Method 2, Replication algorithm 3, marking-finishing algorithm

sub-generation garbage collection algorithm:1, split-collection algorithm


Reference Counting method: in Java, the most direct way to determine whether an object is still needed is to determine if the object has not been referenced. If an object is basically not referenced by any association, proving that the object is unlikely to be referenced again, the object is ten to one that needs to be recycled, and when the object is referenced once, the reference count of the object is added by 1. When an object's reference count is 0, the object is identified as a garbage object at GC, but there is a flaw in the algorithm, which is that if two objects are referenced to each other, the two objects will never be recycled for example:

public static void Main (string[] args) {
    Object Object1=new object ();
    Object Object2=new object ();
    Object1.object=object2;
    Object2.object=object1;
    Object1=null;
    Object2=null;
}
The above objects are no longer possible to use, but their reference count is never 0, so it will never be recycled.

Accessibility Analysis:

To address the circular references that appear above, GC improved after adding a new algorithm, the accessibility algorithm, Java will build a logical object root node gc root by determining whether the object currently can be included in the root node of the reference tree, if not to prove that the current object is also a garbage object

As you can see from the diagram, objects four and five are not included in the root reference tree, so the two objects are judged to be recycled.


mark-Purge algorithm:

When the object is marked out, the next step is to execute, of course, if the implementation of direct traversal, the efficiency is also very low, the tag-purge algorithm is the most basic in the JVM garbage collection algorithm, through the above way, mark the object, directly recycled as shown:


As can be seen from the diagram, the memory space after recycling is uneven, if more than a few times, the memory fragmentation may be too serious to cause the memory of large objects can not be allocated


Replication algorithm:

To address the fragmentation of the above, the original solution is to use memory chunking, the JVM will be divided into two pieces of memory, each one of the space to be recycled, the surviving object to copy to another piece of memory, so that the alternate recovery, to some extent, to avoid large area of memory fragmentation


But this algorithm has a very serious problem, that is, he will lead to a direct reduction in memory space by half, for the memory to live programmers, this is intolerable


tag Collation algorithm:

In order to avoid a large amount of memory waste, and the combination of the above two algorithms, and proposed a new approach. Unlike the tag-purge algorithm, the tag does not clean up the garbage object, but rather to the end of the memory that is identified as the surviving object. Then clear the object outside the end boundary. As shown in figure:



If these are the traditional garbage collection ideas, then this is the JVM's most popular garbage collection method, that is, the use of a generational mechanism after the GC model


Generational collection algorithm:

Generational collection is the method currently used by most JVMs to divide the memory into different regions based on the different lifecycle of the object's survival, and generally divide the GC heap into the younger generation and the old age. The characteristic of the old age is that every time the garbage collection mechanism is triggered, only a small number of objects in the old age need to be recycled, so the different algorithms can be selected for garbage collection according to different regions.

At present, most of the JVMs in the JVM use the replication algorithm for the younger generation, because each GC in the young generation has to recycle a large number of objects, as shown above in the example of the young generation of objects in the Eden area after multiple rounds of GC, still surviving objects will enter the old age, for example:

In the old age, because fewer objects need to be recycled, the tag-sorting algorithm is used generally speaking, object creation is mostly in the younger generation, and very few are directly to the old age, when the new generation of Eden area in the use of low memory will trigger a GC, the survival of the object every age will add one, and copied to the surviving new zone Until it's transferred to the old age area.


To be continued


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.