Java Memory Allocation Policy

Source: Internet
Author: User

Java Memory Allocation Policy
1. objects are preferentially allocated in Eden.

In most cases, objects are allocated in the new generation of Eden. When the Eden area does not have enough space, the virtual machine will initiate a Minor GC. In the following test code, try to allocate 3 2 MB objects and 1 4 MB objects. At runtime, use the parameters-Xmx20M,-Xms20M, -The Xmn10M parameter limits the java heap size to 20 MB and cannot be expanded. 10 MB is allocated to the new generation, and the remaining values are not assigned to the old generation. -XX: Eight vorratio = 8 determines the ratio of the Eden zone in the new generation to the same region vor zone is, that is, Eden: from region vor: to region vor =, that is, 8 MB: 1 MB: 1 MB. The available space for the new generation is 9 MB.

 

Package test1; class TestGc {private static final int _ 1 MB = 1024*1024;/**-verbose: gc-Xms20m-Xmx20m-Xmn10m-XX: + UseSerialGC-XX: + PrintGCDetails-XX: required vorratio = 8 * @ param args */public static void main (String [] args) {byte [] allocation1, allocation2, allocation3, allocation4; allocation1 = new byte [2 * _ 1 MB]; allocation2 = new byte [2 * _ 1 MB]; allocation3 = new byte [2 * _ 1 MB]; allocation4 = new byte [4 * _ 1 MB]; // Monor GC appears once }}

Simple Analysis of the above Code:

Eden: from nation vor: to region vor = 8 MB: 1 MB: 1 MB;

Allocation1, allocation2, and allocation are allocated to Eden, which occupies 6 MB of space. When allocation4 is allocated, it is found that the remaining space of Eden is not allocated 2 MB, so Minor GC occurs, during GC, it is found that all three 2 MB objects cannot be placed in the same vor space (1 MB). Therefore, the warranty mechanism is used to transfer the objects to the old age (3 2 MB objects) in advance ), in this case, the Eden area is restored to 8 Mb space, and allocation4 is allocated to the Eden space.

Running result:

[GC [DefNew: 6487 K-> 160 K (9216 K), 0.0155080 secs] 6487 K-> 6305 K (19456 K), 0.0155918 secs] [Times: user = 0.00 sys = 0.02, real = 0.02 secs]
Heap
Def new generation total 9216 K, used 4584 K [0x000000000000f9a00000, 0x00000000fa400000, 0x000000000000fa400000)
Eden space 8192 K, 54% used[0x00000000f9a00000, 0x00000000f9e51f98, 0x00000000fa200000)
From space 1024 K, 15% used [0x00000000fa300000, 0x000000000000fa3283d0, 0x00000000fa400000)
To space 1024 K, 0% used [0x00000000fa200000, 0x000000000000fa200000, 0x000000000000fa300000)
Tenured generation total 10240 K, used 6144 K [0x000000000000fa400000, 0x000000000000fae00000, 0x000000000000fae00000)
The space 10240 K, 60% used[0x00000000fa400000, 0x00000000faa00030, 0x000000000000faa00200, 0x00000000fae00000)
Compacting perm gen total 21248 K, used 2981 K [0x000000000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
The space 21248 K, 14% used [0x00000000fae00000, 0x000000000000fb0e96a0, 0x00000000fb0e9800, 0x000000000000fc2c0000)
No shared spaces configured.

 

The following result is that the above Code removes the-XX: + UseSerialGC parameter (my environment uses the Parallel Scavenge collector by default), and there is no Minor GC. Why? This is because different collector collection policies are different (you can see the difference between Minor GC and Full GC below ).

Heap
PSYoungGen total 9216 K, used 6651 K [0x000000000000ff600000, 0x0000000100000000, 0x0000000100000000)
Eden space 8192 K, 81% used [0x00000000ff600000, 0x000000000000ffc7eec8, 0x00000000ffe00000)
From space 1024 K, 0% used [0x00000000fff00000, 0x000000000000fff00000, 0x0000000100000000)
To space 1024 K, 0% used [0x00000000ffe00000, 0x000000000000ffe00000, 0x00000000fff00000)
PSOldGen total 10240 K, used 4096 K [0x000000000000fec00000, 0x000000000000ff600000, 0x000000000000ff600000)
Object space 10240 K, 40% used [0x00000000fec00000, 0x000000000000ff000010, 0x00000000ff600000)
PSPermGen total 21248 K, used 2972 K [0x000000000000f9a00000, 0x00000000faec0000, 0x000000000000fec00000)
Object space 21248 K, 13% used [0x00000000f9a00000, 0x000000000000f9ce71e0, 0x00000000faec0000)

Minor GC and Full GC:

New Generation GC (Minor GC ):It only occurs in the new generation of garbage collection, because most java objects have the feature of extinction, and all Minor GC is very frequent, and the collection speed is also relatively fast.

GC (Major GC/Full GC ):Only occurs in the GC of the old generation, and Full GC occurs, often accompanied by at least one Minor GC (but not absolute, in the collection policy of the Parallel Scavenge collector, the Full GC policy selection process is directly performed ). Full GC is generally 10 times slower than Minor GC.

2. large objects directly enter the Old Age

A large object is a Java object that requires a large amount of continuous memory space. The most typical large object is a long string and array. Large objects are bad messages for virtual machine object allocation. large objects often trigger the Garbage Collector in advance to obtain enough continuous space for memory and a lot of space.

 

/**-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];}

Running result:

 

Heap
Def new generation total 9216 K, used 507 K [0x000000000000f9a00000, 0x00000000fa400000, 0x000000000000fa400000)
Eden space 8192 K, 6% used [0x00000000f9a00000, 0x000000000000f9a7ee98, 0x00000000fa200000)
From space 1024 K, 0% used [0x00000000fa200000, 0x000000000000fa200000, 0x00000000fa300000)
To space 1024 K, 0% used [0x00000000fa300000, 0x000000000000fa300000, 0x00000000fa400000)
Tenured generation total 10240 K, used 4096 K [0x000000000000fa400000, 0x000000000000fae00000, 0x000000000000fae00000)
The space 10240 K, 40% used[0x00000000fa400000, 0x00000000fa800010, 0x000000000000fa800200, 0x00000000fae00000)
Compacting perm gen total 21248 K, used 2973 K [0x000000000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
The space 21248 K, 13% used [0x00000000fae00000, 0x000000000000fb0e7428, 0x00000000fb0e7600, 0x000000000000fc2c0000)
No shared spaces configured.

3. Long-lived objects enter the Old Age

Since virtual machines use the idea of generation-based collection to manage memory, memory recycling must be able to identify which objects should be placed in the new generation and which objects should be placed in the old age. To achieve this, the virtual machine defines an object Age counter for each object. If the object is still alive after being born in Eden and has undergone a Minor GC and can be accommodated by the same vor, it will be moved to the same vor space and the age is set to 1. the Minor GC is performed every time in a vor. The age of the object is added to a certain degree (15 by default) and will be promoted to the old age.

The threshold value of an object in the old age can be set using the-XX: MaxTenuringThreshold parameter.

4. Dynamic Object age determination

To better adapt to the memory conditions of different programs, virtual machines do not always require that the age of the object must reach MaxTeburingThreshold to be promoted to the old age, if the total size of all objects of the same age in the primary vor space is half of that of the multi-dimensional vor space, objects of the same age or age can directly enter the old age, you do not need to wait until the age specified in MaxTenuringThreshold.

5. space allocation guarantee

Before MInor GC occurs, the virtual opportunity first checks whether the maximum available continuous space in the old age is greater than the sum of all objects in the new generation. If this condition is true, Minor GC can ensure security. If this parameter is not set, you can run the "HandelPromotionFailure" command to check whether the guarantee fails. If yes, check whether the available space in the old age is larger than the average size of objects promoted to the old age. If yes, try Minor GC once, although this Minor GC is risky, if the value is smaller than or HandelPromotionFailure setting does not allow risk taking, it will also require a Full GC.

-XX:-HandelPromotionFailure = false.

 

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.