Java Virtual Machine (3)--memory allocation and recovery policy

Source: Internet
Author: User

Three, memory allocation and recovery Strategy 1.1 Minor GC and full GC
    • Minor GC: Occurs in the Cenozoic, because the new generation of objects survival time is very short, so the Minor GC will be executed frequently, the speed of execution will generally be relatively fast.
    • Full GC: In the old age, the old age object has a long time to live, so the full GC rarely executes and executes much slower than the Minor GC.
1.2 Memory allocation policy 1.2.1 object precedence in Eden assignment

In most cases, objects are allocated in the new generation of Eden, and when the Eden area is not space enough, the Minor GC is initiated.

1.2.2 Large objects directly into the old age

Large objects are objects that require contiguous memory space, and the most typical large objects are the long strings and arrays.

Frequent large objects trigger garbage collection ahead of time to get enough contiguous space to allocate to large objects.

-xx:pretenuresizethreshold, objects larger than this value are allocated directly in the old age, avoiding large amounts of memory duplication between the Eden and Survivor areas.

1.2.3 long-lived objects into the old age

Define age counters for objects, objects that were born in Eden and survived by Minor GC, will move to Survivor, age increases by 1 years, and increases to a certain age, moving to the old era.

The-xx:maxtenuringthreshold is used to define the age threshold.

1.2.4 Dynamic Object Age judgement

The virtual machine does not always require that the age of the object must reach maxtenuringthreshold to be promoted to the old age, if the sum of all objects of the same age in Survivor is greater than half the Survivor space, Objects older than or equal to that age can go straight into the old age without waiting for the ages required in the maxtenuringthreshold.

1.2.5 Space Allocation Guarantee

Before the Minor GC occurs, the virtual machine checks whether the largest available contiguous space in the old age is greater than the total space of all new generation objects, and if the condition is true, then the Minor GC can be identified as safe.

If not, virtual opportunity to see if the Handlepromotionfailure setting value allows the guarantee to fail, and if so, then continue to check whether the largest available contiguous space in the old age is greater than the average size of the previous promotion to the old age object, and if it is greater than, try to make a Minor GC, if it is less than, or the Handlepromotionfailure setting does not allow adventure, then a full GC will be performed.

1.3 Full GC trigger conditions

For Minor GC, the triggering condition is very simple, and when the Eden space is full, a Minor GC is triggered. The full GC is relatively complex and has the following conditions:

1.3.1 Call System.GC ()

It is only recommended that the virtual machine perform a full GC, but the virtual machine does not necessarily actually execute. It is not recommended to use this approach, but rather to have the virtual machine manage memory.

1.3.2 Old age Space shortage

The common scenario for the old age space shortage is that the large objects mentioned above go directly into the old age, and the long-term surviving objects enter the old age.

To avoid the full GC caused by these reasons, you should try not to create oversized objects and arrays. In addition, the-XMN virtual machine parameters can be adjusted to the size of the new generation, so that the object as far as possible in the Cenozoic was recycled, not into the old age. It is also possible to move large objects into the old age by-xx:maxtenuringthreshold, allowing objects to survive in the Cenozoic for a period of time.

1.3.3 Space Allocation guarantee failed

The Minor GC using the replication algorithm requires the memory space of the old age to be secured, and if the guarantee fails, a full GC is executed. Please refer to section Fifth above for details.

1.3.4 JDK 1.7 and previous permanent generation space is low

In JDK 1.7 and before, the method area in the HotSpot virtual machine is implemented by the permanent generation, and the data such as Class information, constants, static variables are stored in the permanent generation.

When the system is loaded with classes, reflected classes, and calls to many methods, the permanent generation may be full, and if it is not configured to adopt a CMS GC, it will be executed. If the full GC is still not recycled, then the virtual opportunity throws java.lang.OutOfMemoryError.

To avoid the above cause of the full GC, you can use the method to increase the permanent generation of space or to use the CMS GC.

1.3.5 Concurrent Mode Failure

In the process of executing the CMS GC, there are objects to be put into the old age, and at this time the old age space is insufficient (may be the GC process of excessive floating garbage resulting in a temporary lack of space), will be reported Concurrent Mode Failure error, and trigger the full GC.

Java Virtual Machine (3)--memory allocation and recovery 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.