Java Memory allocation policy

Source: Internet
Author: User

1. Object Precedence in Eden Assignment

In most cases, objects are allocated in the Cenozoic Eden region. When there is not enough space in the Eden area, the virtual machine will initiate a minor GC. In the following test code, try to allocate 3 2MB size and a 4MB size object, at run time through the parameter-xmx20m,-xms20m,-xmn10m these three parameters limit the Java heap size is 20MB, not extensible, where 10MB is allocated to the Cenozoic, The rest of the non-rationing old age. -xx:survivorratio=8 determines that the ratio of Eden in the Cenozoic to a Survivor area is 8:1, that is, eden:from survivor:to Survivor = 8:1:1, or 8MB:1MB:1MB, The new generation of free space is 9MB.

Package Test1;class TESTGC {private static final int _1mb = 1024x768 * 1024;/**-verbose:gc-xms20m-xmx20m-xmn10m-xx:+useserial Gc-xx:+printgcdetails-xx:survivorratio=8 * @param args */public static void main (string[] args) {byte[] allocation1, Allo Cation2, Allocation3, Allocation4; Allocation1  = new byte[2 * _1MB]; Allocation2  = new byte[2 * _1MB]; Allocation3  = new byte[2 * _1MB]; Allocati On4  = new Byte[4 * _1mb];//appears once Monor GC}}

A simple analysis of the preceding section of code:

Eden:from survivor:to Survivor = 8MB:1MB:1MB;

Allocates Allocation1, Allocation2, allocation three objects to the Eden area, which accounts for 6MB space, and when allocating Allocation4, discovers that Eden's remaining space is not 2MB enough to be allocated, so minor GC occurs, During the GC, it was found that the existing 3 2MB objects could not be placed into the Survivor Space (1MB), so the guarantee mechanism was moved forward to the old age zone (3 2MB objects), when the Eden Zone reverted to 8MB space and then the Allocation4 was assigned to Eden Space.

Operation Result:

[GC [defnew:6487k->160k (9216K), 0.0155080 secs] 6487k->6305k (19456K), 0.0155918 secs] [times:user=0.00 sys=0.02, real=0.02 secs]
Heap
def New Generation Total 9216K, used 4584K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
Eden Space 8192K, 54% used[0x00000000f9a00000, 0x00000000f9e51f98, 0x00000000fa200000]
From space 1024K, 15% used [0x00000000fa300000, 0x00000000fa3283d0, 0x00000000fa400000)
To space 1024K, 0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
Tenured generation total 10240K, used 6144K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
the space 10240K, 60% used[0x00000000fa400000, 0x00000000faa00030, 0x00000000faa00200, 0x00000000fae00000]
Compacting Perm Gen Total 21248K, used 2981K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
The space 21248K, 14% used [0x00000000fae00000, 0x00000000fb0e96a0, 0x00000000fb0e9800, 0x00000000fc2c0000)
No shared spaces configured.


The result is that the above code is stripped of the-XX:+USESERIALGC parameter (my environment defaults to the parallel scavenge collector), and no minor GC occurs. This is because different collector collection strategies are different (you can see the difference between the minor GC and the full GC below).

Heap
Psyounggen total 9216K, used 6651K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
Eden Space 8192K, 81% used [0x00000000ff600000,0x00000000ffc7eec8,0x00000000ffe00000)
From space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
To space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
Psoldgen total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
Object Space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
Pspermgen total 21248K, used 2972K [0x00000000f9a00000, 0x00000000faec0000, 0x00000000fec00000)
Object Space 21248K, 13% used [0x00000000f9a00000,0x00000000f9ce71e0,0x00000000faec0000)

Minor GC and full GC:

The new generation GC (Minor GC): only occurs in the new generation of garbage collection actions, because most Java objects have the characteristics of the Minor, all the GC is very frequent, the general recovery speed is relatively fast.

old age GC (Major gc/full GC): only in the old GC, there is a full GC, often accompanied at least once minor GC (but not absolute, in parallel scavenge The collector's collection strategy has a policy selection process that directly carries out full GC. The full GC is typically 10 times times slower than the minor GC.

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 object assignment of a virtual machine, and often large objects tend to cause memory and a lot of space to trigger the garbage collector ahead of time to get enough contiguous space to accommodate them.

/**-VERBOSE:GC  -xms20m-xmx20m-xmn10m-xx:+printgcdetails-xx:survivorratio=8-xx:+useserialgc-xx: pretenuresizethreshold=3145728 (3MB) */public static void Test2 () {byte[] allocation1; allocation1  = new Byte[4 * _1MB ];}

Operation Result:

Heap
def New Generation Total 9216K, used 507K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
Eden Space 8192K, 6% used [0x00000000f9a00000, 0x00000000f9a7ee98, 0x00000000fa200000)
From space 1024K, 0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
To space 1024K, 0% used [0x00000000fa300000, 0x00000000fa300000, 0x00000000fa400000)
Tenured generation total 10240K, used 4096K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
the space 10240K, 40% used[0x00000000fa400000, 0x00000000fa800010, 0x00000000fa800200, 0x00000000fae00000]
Compacting Perm Gen Total 21248K, used 2973K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
The space 21248K, 13% used [0x00000000fae00000, 0x00000000fb0e7428, 0x00000000fb0e7600, 0x00000000fc2c0000)
No shared spaces configured.

3. Long-term survival of the object into the old age

Now that the virtual machine uses the idea of generational collection to manage memory, 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 Eden was born and after a Minor GC, and can be accommodated by survivor, it will be moved to survivor space, and the age is set to 1. Object in survivor every time Minor GC, Object age plus 1 years old, The age is added to a certain level (by default, 15) and is promoted to the old age.

The threshold of the age of the object can be set by the parameter-xx:maxtenuringthreshold.

4. Dynamic Object Age judgment

In order to better adapt to the memory status of different programs, the virtual machine is not always required the age of the object must reach maxteburingthreshold to be promoted to the old age, if the survivor space of all objects of the same age in the sum of survivor space half, Objects older than or equal to that age can enter the old age without waiting for the age required in the maxtenuringthreshold.

5. 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 sum of all the objects in the new generation, and if this condition is true, then the minor GC can ensure that it is safe. If not, the virtual opportunity to see if the Handelpromotionfailure setting value allows the warranty to fail. If allowed, then will continue to check whether the continuous space available 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 minor GC once, although this time the minor GC is risky If the less than or handelpromotionfailure setting does not allow the risk, it is also going to be a full GC.

-xx:-handelpromotionfailure=false.


Java Memory allocation policy

Related Article

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.