JVM Summary (ii): JVM's memory allocation policy

Source: Internet
Author: User

In this section we summarize the memory allocation policy in the JVM. The directory is as follows:

Memory allocation policy
Object first in New Generation Eden Assignment
Large objects go straight into the old age
Long-lived objects will enter the old age.
Dynamic Object Age Determination
Space allocation guaranteed memory allocation policy

The automatic memory management advocated in the Java technology system can be attributed to two parts: allocating memory to objects and reclaiming memory allocated to objects.
As we all know, Java object allocations are allocated on the Java heap, although there is a JIT compilation that is split into scalar types and is briefly allocated on the stack. If the generational algorithm is used, then the new object is allocated in the Cenozoic Eden region. If the local thread allocation buffer is started, it will be assigned by thread first on Tlab.
In fact, Java's allocation rules are not hundred percent fixed, depending on which garbage collector combination is currently being used, and the settings of memory-related parameters in the virtual machine.
Below we have talked about the serial and serial old collectors in the above section to do a memory allocation and recycling strategy summary.

Object first in New Generation Eden Assignment

First, let's look at the new generation of memory allocations:

Memory allocation situation:
Divide the JVM memory into a larger Eden space (80%) and two small servivor (10% each). When recycled, the objects that are still alive in Eden and survivor are copied directly to another servivor space using the copy algorithm, and finally the data in the yard Eden Space and the original survivor space are cleared.
In most cases, objects are allocated in the Cenozoic Eden region. When the Eden area does not have enough space to allocate, the JVM initiates a minor GC.
Here are the first two concepts:

    • New Generation GC (Minor GC): Refers to the garbage collection action that occurs in the Cenozoic, because Java objects are mostly characterized by an out-of-date, so Minor GC is very frequent, and the speed is relatively fast.
    • Old age GC (Major gc/full GC): Refers to the GC, which occurred in the old age, the Major GC, which may also be accompanied by a minor GC, but unlike the minor GC, Major GC is 10 times times slower than the speed.
Large objects go straight into the old age

Let's start with a definition of a large object: A large object, which refers to a Java object that requires a lot of contiguous memory space. The most typical large object can be a long string and an array.
The JVM's attitude toward large objects:
Large objects are very cumbersome for the memory allocation of the JVM, and if we allocate large objects in the Cenozoic, it can easily lead to a lot of memory space to trigger garbage collection ahead of time to get enough contiguous space to "place" them. ,
In order to avoid the resources and time wasted by unwanted GC activities that often occur in this situation, the allocation strategy is to allocate large objects directly into the old age, and the-xx:pretenuresizethreshold parameters are also provided in the virtual machine. Objects larger than this set value are allocated content directly in the old age.

Long-lived objects will enter the old age.

When the JVM uses the idea of generational collection to manage memory, the JVM defines an object age counter for each object in order to identify which objects should be placed in the new generation and which objects should be in the old age.
Object Age Counter: If the object is still alive after the first minor GC is born in Eden and can be accommodated by survivor, it can be moved to survivor space. The age counter sets the age of the object to 1. For objects in the Survivor area, the age increases by 1 years after each minor GC, and when its age increases to a certain level (which can be set by the parameter-xx:maxtenuringthreshold), the object enters the old age. Become the object of the old age.

Dynamic Object Age Determination

In fact, some virtual machines do not always require that the age of the object must reach maxteruringthreshold to be promoted to the old age, if the sum of all objects in survivor space is greater than half the size of surivior space, Objects older than or equal to that age can be directly carried out in the old age, without waiting for the ages required in the maxteruringthreshold.

Space Allocation guarantee

Before the minor GC occurs, the virtual opportunity first checks whether the largest available contiguous space in the old age is greater than the total space of all objects in the Cenozoic, and if this condition is true, then the minor GC can ensure that it is safe and, if not, The virtual opportunity to see if the Handlepromotionfaiure setting value allows the warranty to fail. If allowed, then will 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 GC is risky; or the Handlepromotionfaiure setting does not allow the risk, then a full GC will be performed instead.
The so-called adventure: that is, when used to rotate the survivor area can not withstand the survival of the new generation of memory, the need for the old age distribution guarantee, the survivor can not accommodate the object directly into the old age, provided that the old age

JVM Summary (ii): JVM's memory allocation 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.