Java Virtual machine: JVM Memory generational policy

Source: Internet
Author: User

The Java Virtual machine divides the heap memory into several blocks according to the life cycle of the object, which is generally divided into the Cenozoic, the old age and the permanent generation (to the Hotspot virtual machine), which is the memory generational strategy of the JVM.

why generational?

Heap memory is the largest piece of memory managed by a virtual machine and is the most frequently garbage collected area, and all of our program object instances are stored in heap memory. Heap memory is allocated for the purpose of increasing the efficiency of object memory allocation and garbage collection. Imagine that if heap memory is not partitioned, all newly created objects and long-life objects are put together, and as the program executes, heap memory needs to be garbage collected frequently, and each time the collection iterates through all the objects, the time it takes to traverse those objects is huge and can seriously affect our GC efficiency. This is horrible.

With memory generation, the situation is different, the newly created objects will be allocated in the new generation of memory, after multiple recovery of the surviving objects are stored in the old age, static properties, class information, such as stored in the permanent generation, the new generation of the object survival time is short, only in the Cenozoic region to frequent GC, In the old age, the object life cycle is long, the frequency of memory recovery is relatively low, do not need to recycle frequently, the recovery effect in the permanent generation is too poor, generally do not carry out garbage collection, but also according to the characteristics of different ages to adopt appropriate garbage collecting algorithm. Collection efficiency is greatly enhanced by generational collection, which is the benefit of memory generational.

        memory generational Division

The Java Virtual machine divides the heap memory into the new generation, the old age and the permanent generation , the permanent generation is the hotspot virtual machine unique concept, it uses the permanent generation way to realize the method area, the other virtual machine implementation does not have this concept, And the hotspot also has the tendency to cancel the permanent generation, in JDK 1.7 the hotspot has already begun "go permanent", the string constant pool that originally placed in the permanent generation is moved out. Permanent generation of the main storage constants, class information, static variables and other data, and garbage collection is not a big relationship, the new generation and the old age is the main area of garbage collection. Memory generational is as follows:

       650) this.width=650; "src=" http:// Images2015.cnblogs.com/blog/952935/201707/952935-20170719161150740-1984455628.jpg "style=" margin:0px;padding:0 px;border:0px; "/>

New generation (young)

The newly-generated objects are first stored in the Cenozoic, the new generation objects face to death, the survival rate is very low, in the Cenozoic, the general application of a garbage collection generally can reclaim 70% ~ 95% of space, recovery efficiency is very high.

       hotspot divides the Cenozoic into three blocks, a larger Eden Space and two smaller survivor spaces, The default scale is 8:1:1. The purpose of the division is that the hotspot uses a replication algorithm to recover the new generation, which is set up to make full use of memory space and reduce waste. The newly generated objects are allocated in the Eden area (except for large objects, large objects go straight into the old age), and when the Eden Zone does not have enough space to allocate, the virtual machine will initiate a minor GC.

       GC at the beginning, objects will only exist in the Eden and from survivor areas, to The Survivor area is empty (as a reserved area). When the GC is in progress, all surviving objects in the Eden area are copied to the to Survivor area, and in the From Survivor area, the surviving objects will be determined according to their age values to reach the age threshold (default is 15, the new generation of objects every time a cycle of garbage collection, The age value is added 1,GC The age of the objects stored in the header of the object is moved to the old age, and objects that do not reach the threshold are copied to the to Survivor area. Then empty the Eden and from survivor areas, and the surviving objects in the Cenozoic are in the to Survivor area. Next,  from survivor and to survivor will exchange their roles, i.e. the new to Survivor area is the from Survivor area where the last GC was emptied, and the new from Survivor area is the last GC's to Survivor District, in short, will ensure that the to survivor area is empty after a round of GC. GC when there is not enough space in the to Survivor area to store the last generation of surviving objects collected, it is necessary to rely on the old age for distribution guarantees to store these objects in the old age.

older generation (old)

in the new generation, the objects that survived after the GC (specifically the threshold of the virtual machine configuration) were passed into the old age. In the old age, the object life cycle is longer, the survival rate is relatively high, the frequency of GC in the old age is relatively low, and the speed of recovery is relatively slow.

permanent Generation (Permanent)

data such as permanent storage class information, constants, static variables, instant compiler compiled code, and so on, for this region, the Java Virtual Machine specification indicates that garbage collection is not possible and is generally not garbage collected.

       minor GC and full GC

New Generation GC (Minor GC): Minor GC refers to a new generation of GC, because most of the new generation of Java objects are going to die, so Minor GC is very frequent, the general recovery speed is relatively fast. Minor GC is triggered when Eden space is insufficient to allocate memory for an object.

Old age GC (full gc/major GC): Full GC refers to the GC that occurs in the old age, where full GC is generally accompanied by at least one minor GC (the object of the old age is most of the minor GC process from the Cenozoic into the old age), such as: the allocation of guarantees failed. The full GC is typically 10 times times slower than the minor GC. The full GC is triggered when the old age is low on memory or when the System.GC () method is explicitly called.


Java Virtual machine: JVM Memory generational policy

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.