In-depth understanding of Java Virtual Machines notes chapter III memory allocation and recovery strategies

Source: Internet
Author: User

Several of the main most common memory allocation rules:

??

1. Object Precedence in Eden assignment

?

In most cases, objects are allocated in the new generation of Eden .

??

When the Eden area does not have enough space to allocate, the virtual will initiate a Minor GC, and if the surviving objects in the post-generation GC cannot be fully placed in the Survivor space, It is necessary to advance into the old age by assigning a guarantee mechanism, provided that all surviving objects cannot be accommodated in the old age, that is, the only part can be accommodated .

??

The surviving objects that failed to enter the old age will continue to be allocated in the Eden area.

??

If the Eden Zone also fails to accommodate the remaining surviving objects, the virtual machine throws a outofmemoryerror error

??

The virtual machine provides the -xx:+printgcdetails parameter for output collector log parameters.

??

Minor The difference between GC and full GC :

??

A. New Generation GC (Minor GC): refers to the garbage collection action occurring in the new generation, because most Java objects have the characteristics of being born to die, so Minor GC is very frequent , generally recovery speed is also relatively fast .

??

B. Old age GC (Major gc/full GC): refers to GC, which occurs in the old age , Major GC, often accompanied at least once Minor GC ( But not absolutely, in the parallelscavenge Collector's collection strategy, there is a policy selection process that directly carries out the Major GC .

??

The speed of MAJORGC is generally more than the MINORGC of more than ten times.

??

??

2. large objects directly into the old age

?

A large object is a Java object that requires a large amount of contiguous memory space

??

The most typical large object is the long string and array .

??

A large object is a bad message for the memory allocation of a virtual machine

??

Often, large objects tend to cause memory and a lot of space to trigger garbage collection ahead of time to get enough contiguous space to "place" them .

?

??

The virtual machine serves as a -xx:pretenuresizethreshold parameter, making the object larger than this setting value allocated directly in the old age .

??

This is done to avoid a large amount of memory duplication between the Eden area and the two Survivor zones .

??

3. long-term survival targets will enter the old age

?

Virtual machines Use the idea of generational collection to manage memory, so memory recycling must be able to identify which objects should be placed in the new generation and which should be in the old age.

??

To do this, the virtual machine defines an Object age counter for each object .

??

If the object is still alive after the first Minor GC is born in the Eden area and can be accommodated by the Survivor zone, it will be moved to the Survivor area and The object age is set to 1.

??

Object in the Survivor area each time Minor GC, age increases by 1 years .

??

When its age increases to a certain extent ( by default, it is a year old ), it is promoted to the old age.

??

The age threshold of an object's promotion to the old age can be set by parameter -xx:maxtenuringthreshold .

??

4. Dynamic Object Age Determination

??

In order to better adapt to the memory situation of different programs, the virtual machine does not always require that the object age must reach maxtenuringthreshold to be promoted to the elderly

??

If the sum of all objects of the same age in the Survivor space is greater than half the size of Survivor space, then objects older than or equal to that age will go directly to the old era without waiting for The age required in the maxtenuringthreshold.

??

5. Space Allocation guarantee

?

When a Minor GC occurs, the average size of each promotion to the old age is greater than the size of the remaining space in the old age before the virtual opportunity is detected

??

If greater than, the full GC is changed to straight pull

??

If it is less than, see if the handlepromotionfailure setting allows the warranty to fail;

??

If allowed, the Minor GCis only performed, and if not allowed, a full GCis performed instead.

?

The Cenozoic uses the Replication collection algorithm, but in order to improve memory utilization, only one of the Survivor spaces is used as a rotational backup

??

Therefore, when a large number of objects still survive after the Minor GC , the most need for the old age to allocate security, so that the Survivor space can not accommodate objects directly into the old age.

?

Averaging is still a means of dynamic probability, which means that if a Minor GC survives a sudden increase, much higher than the average, it will still lead to a guarantee failure (handlepromotionfailure). If there is a handlepromotionfailure, then you have to re-launch the full GCafter the failure. Although it is the largest in the circles when the guarantee fails, the handlepromotionfailure switch is opened in large cases to avoid the full GC being too frequent.

In-depth understanding of Java Virtual Machines notes chapter III memory allocation and recovery strategies

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.