In-depth understanding of Java Virtual Machine notes---memory allocation and recycling strategies

Source: Internet
Author: User

Automatic memory management in the Java technology system ultimately boils down to solving two problems automatically: allocating memory to objects and reclaiming memory allocated to objects. The memory allocation of an object is allocated on the heap in a large direction, and the object is allocated primarily on the new generation of Eden, and if the local thread allocation buffer is started (-xx:+usetlab, which is turned on by default), it will be allocated on Tlab by thread precedence. In a few cases it may also be assigned directly in the old age, the allocation rules are not completely fixed, the details depend on which garbage collector combination is currently in use, and the memory-related parameter settings in the virtual machine.
Here are a few 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 Zone 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 into the survivor space, they need to be pre-entered into the old age by assigning a security mechanism. 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 dying, so Minor GC is very frequent, the general recovery speed is relatively fast.
B. The 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 absolute, in the Parallelscavenge Collector's collection strategy directly Major GC's policy selection process). The speed of MAJORGC is generally more than 10 times times slower than MINORGC.


2. Large objects directly into the old age
A large object is a Java object that requires a lot of contiguous memory space, and the most typical large object is the long string and array. Large objects are bad news for the memory allocation of virtual machines, and 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 area, it will be moved to the survivor area and the object age will be set to 1. Object in the Survivor area each time minor GC, age increases by 1 years. When its age increases to a certain level (by default, 15 years 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 old age, if the sum of all object sizes in survivor space is greater than half of survivor space, Then the age of the object is greater than or equal to the old age, without waiting for the age required by Maxtenuringthreshold.

5. Space Allocation Guarantee
When the 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, and if it is greater, 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, only minor GC is performed, and if not allowed, a full GC is 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, so when a large number of objects survive after the minor GC, the old age is most needed for distribution guarantees, Let the survivor space can not accommodate the object 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 GC after 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 Machine notes---memory allocation and recycling 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.