On Java memory allocation and recovery strategy

Source: Internet
Author: User
Tags xms

First, Introduction

In the Java technology system mentioned in the final analysis of memory automation management is the allocation and recovery of memory two problems, before and we have talked about the knowledge of Java recycling, today to talk about the Java object in memory allocation. Popularly speaking, the object's memory allocation is the allocation on the heap, the object is mainly distributed in the new Generation of Eden (on the memory of the object in the garbage collection will be made up, want to know can also refer to "in-depth understanding of Java Virtual Machine"), if the local thread allocation buffer is started, The Tlab is assigned on a per-thread basis. In a few cases it is also directly distributed in the old age.

Second, the classic allocation strategy 1, the object priority is allocated on Eden

In general, objects are preferentially allocated on Eden, and when Eden does not have enough space to allocate, the JVM initiates a minor GC. If there is still not enough space to allocate, there are additional measures in the back, which are mentioned below.

Set the virtual machine's even log parameter-xx:+printgcdetails, which prints the recycle log of the memory at garbage collection, and outputs the allocation of the current memory area when the process exits. The following is a specific example, the first need to set the JVM parameter-xms20m-xmx20m-xmn10m, these three parameters that the Java heap size is 20M, and is not extensible, where 10M is allocated to the new generation, the remaining 10M allocated to the old age. The-xx:survivorratio=8 is the default of the JVM for the new generation of Eden and survivor ratios, which defaults to 8:1. The reason is that the new generation of object 98% will be recycled in the next GC, so it is suitable to use the replication algorithm for garbage collection, so the new generation of 10M of memory, 8M is eden,1m is survivor, and the other 1M is not used with the copy algorithm memory block, but also survivor.

1  Public classReflecttest {2 3     Private Static Final int_1MB = 1024*1024;4     5      Public Static voidtestallocation () {6         byte[] allocation1, Allocation2, Allocation3, Allocation4;7Allocation1 =New byte[2 *_1MB];8Allocation2 =New byte[2 *_1MB];9Allocation3 =New byte[2 *_1MB];TenAllocation4 =New byte[6 *_1MB]; One     } A      -      Public Static voidMain (string[] args) { - reflecttest.testallocation (); the     } -      -}

The output is as follows

Heap Psyounggen Total 9216K, used 6651K [0x000000000b520000, 0x000000000bf20000, 0x000000000bf20000) Eden Space 8192K,81% used [0x000000000b520000,0x000000000bb9ef28,0x000000000bd20000) from Space 1024K,0% used [0x000000000be20000,0x000000000be20000,0x000000000bf20000) to space 1024K,0% used [0x000000000bd20000,0x000000000bd20000,0x000000000be20000) Psoldgen total 10240K, used 6144K [0x000000000ab20000, 0x000000000b520000, 0x000000000b520000) object Space 10240K,60% used [0x000000000ab20000,0x000000000b120018,0x000000000b520000) Pspermgen total 21248K, used 2973K [0x0000000005720000, 0x0000000006be0000, 0x000000000ab20000) object Space 21248K,13% used [0x0000000005720000,0x0000000005a07498,0x0000000006be0000)

You can see that Eden occupies 81%, indicating that Allocation1, Allocation2, Allocation3 are all allocated to the new generation of Eden.

2. Direct allocation of large objects in the old age

Large objects are objects that require a lot of contiguous memory space to be stored, similar to the very long string and array. Large objects are not good for the memory distribution of virtual machines, and when it comes to many large object JVMs that survive only one round, it is more difficult to write code that avoids such problems. The-xx:pretenuresizethreshold parameter is provided in the virtual machine, and an object greater than this value is allocated directly to the old age, and this is done to avoid a large amount of memory copy occurring between the Eden and survivor areas. In the previous garbage collection algorithm replication algorithm mentioned, there is not much to say.

 Public classReflecttestbig {Private Static Final int_1MB = 1024*1024;  Public Static voidtestallocation () {byte[] allocation2, Allocation3, Allocation4;Allocation2 =New byte[2 *_1MB]; Allocation3=New byte[2 *_1MB]; Allocation4=New byte[6 *_1MB]; }         Public Static voidMain (string[] args) {reflecttestbig.testallocation (); }    }

The output is as follows

Heap Psyounggen Total 8960K, used 4597K [0x000000000b510000, 0x000000000bf10000, 0x000000000bf10000) Eden Space 7680K,59% used [0x000000000b510000,0x000000000b98d458,0x000000000bc90000) from Space 1280K,0% used [0x000000000bdd0000,0x000000000bdd0000,0x000000000bf10000) to space 1280K,0% used [0x000000000bc90000,0x000000000bc90000,0x000000000bdd0000) Psoldgen total 10240K, used 6144K [0x000000000ab10000, 0x000000000b510000, 0x000000000b510000) object Space 10240K,60% used [0x000000000ab10000,0x000000000b110018,0x000000000b510000) Pspermgen total 21248K, used 2973K [0x0000000005710000, 0x0000000006bd0000, 0x000000000ab10000) object Space 21248K,13% used [0x0000000005710000,0x00000000059f7460,0x0000000006bd0000)

Can see Allocation4 has exceeded the set of-xx:pretenuresizethreshold=3145728, arbitrarily allocation4 directly assigned to the old age, the old age occupancy rate of 60%. Note that this setting-xx:pretenuresizethreshold=3145728 cannot be written as-xx:pretenuresizethreshold=3m, otherwise the JVM will not be recognized.

3, long-term survival of the object will enter the old age

Since the virtual machine uses the idea of a split-band collection to manage memory, memory recycling must identify which objects should be placed in the new generation and which should be in the old age. To achieve this, the JVM defines an age counter for each object. If the object survives after Eden is born and can survive the first minor GC, and can be stored in survivor, it will be moved to survivor and the age of the object will be set to 1. Each time the object escapes minor GC, the age increases by 1, and when his age exceeds the threshold of one year, the object is promoted to the old age. This threshold JVM defaults to 15 and can be set by-xx:maxtenuringthreshold.

 Public classJavatest {Static intm = 1024 * 1024;  Public Static voidMain (string[] args) {byte[] A1 =New byte[1 * M/4]; byte[] A2 =New byte[7 *m]; byte[] A3 =New byte[3 * m];//GC    }  }

The output is as follows

[GC [defnew:7767k->403k (9216K), 0.0062209 secs] 7767k->7571k (19456K), 0.0062482secs] [Times:user=0.00 sys=0.00, real=0.01secs] A3 OK Heap defNewGeneration Total 9216K, used 3639K [0x331d0000, 0x33bd0000, 0x33bd0000) Eden Space 8192K,39% used [0x331d0000, 0x334f9040, 0x339d0000) from Space 1024K,39% used [0x33ad0000, 0x33b34de8, 0x33bd0000) to space 1024K,0% used [0x339d0000, 0x339d0000, 0x33ad0000) tenured generation total 10240K, used 7168K [0x33bd0000, 0x345d0000, 0x345d0000) The space 10240K,70% used [0x33bd0000, 0x342d0010, 0x342d0200, 0x345d0000) Compacting Perm gen Total 12288K, used 381K [0x345d0000, 0x351d0000, 0x385d0000) The space 12288K,3% used [0x345d0000, 0x3462f548, 0x3462f600, 0x351d0000) Ro space 10240K,55% used [0x385d0000, 0x38b51140, 0x38b51200, 0x38fd0000) RW space 12288K,55% used [0x38fd0000, 0x396744c8, 0x39674600, 0x39bd0000)

Can see A2 already survived once, age is 1, meet the set of-xx:maxtenuringthreshold=1, so A2 into the old age, and A3 into the new generation.

4. Dynamic Object Age Determination

In order to better adapt to the memory state of different programs, the virtual machine does not always require that the age of the object must reach the value set by-xx:maxtenuringthreshold to be promoted to the old age, If the sum of all objects of the same age in the survivor space is greater than half the size of survivor space, objects older than or equal to that age can go directly into the old age area without having to reach the setting value in-xx:maxtenuringthreshold.

5. Space Allocation Guarantee

In the case of minor GC, the virtual opportunity detects whether the average size of each promotion to the old age is greater than the remaining space in the old age, and if it is greater then a full GC is performed directly. If it is less than, see if the Handlerpromotionfailyre setting allows the warranty to fail, and if it is allowed, only the minor GC, and the full GC is improved if not allowed. This means that the new generation of Eden will be stored in the old age when the object is not saved.

Iii. Common JVM parameter settings

1,-xms: The initial heap size, the default (minheapfreeratio parameter can be adjusted) when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx.

2, XMX: The maximum heap size, the default (maxheapfreeratio parameter can be adjusted) when the free heap memory is greater than 70%, the JVM will reduce the heap until the minimum limit of-XMS.

3,-xmn: Young Generation size (1.4or lator), here the size is (eden+ 2 survivor Space). The new gen shown in Jmap-heap is different.
The entire heap size = younger generation size + old generation size + persistent generation size.
Increasing the younger generation will reduce the size of older generations. This value has a large impact on system performance, and Sun is the official recommended configuration for the entire heap of 3/8.

4,-xx:newsize: Set the young generation size (for 1.3/1.4).

5,-xx:maxnewsize: Young generation Maximum (for 1.3/1.4).

6,-xx:permsize: Set the persistent generation (Perm Gen) initial value.

7.-xx:maxpermsize: Sets the maximum value of the persistent generation.

8,-XSS: The stack size of each thread, JDK5.0 after each thread stack size is 1M, the previous thread stack size is 256K. The memory size required for the thread to be applied is adjusted. Under the same physical memory, Reducing this value can generate more threads. However, the operating system of the number of threads within a process is still limited, can not be generated indefinitely, the empirical value of 3000~5000 around.

9.-xx:newratio: The ratio of young generations (including Eden and two survivor districts) to older generations (except for persistent generations),-xx:newratio=4 that the ratio of young generations to older generations is 1:4, and the younger generation accounts for 1/5 of the entire stack. Xms=xmx and the xmn is set, this parameter does not need to be set.

10,-xx:survivorratio:eden area and survivor area size ratio, set to 8, then two Survivor area and an Eden area ratio of 2:8, a survivor area accounted for the entire young generation of 1/10.

11,-xx:largepagesizeinbytes: The size of the memory page can not be set too large, will affect the size of perm.

12,-XX:+DISABLEEXPLICITGC: Close System.GC ()

13,-xx:maxtenuringthreshold: The maximum age of garbage, if set to 0, then the younger generation of objects do not go through the survivor area, directly into the old generation. For older generations of more applications, can improve efficiency. If you set this value to a larger value, the younger generation object will replicate multiple times in the Survivor area, which can increase the lifetime of the object's younger generations, increasing the probability that it will be recycled in the younger generation. The parameter is valid only for serial GC.

14,-xx:pretenuresizethreshold: The object is more than how much is directly in the old generation allocation, the new generation of unit bytes using parallel scavenge GC when the other one directly in the old generation of the allocation of the case is a large array of objects, and there is no external reference object in the array.

15.-xx:tlabwastetargetpercent:tlab% of Eden area.

Iv. Supplementary

Minor the difference between GC and full GC:

New Generation GC (Minor GC): Refers to the garbage collection action occurring in the Cenozoic, because the large logarithm of the Java object is not the first round of GC, so the Minor GC is used very frequently, the general recovery speed is relatively fast.

Old age GC (full gc/major GC): Refers to the GC that occurred in the old age, Major GC, often accompanied at least once minor GC (but not absolute, in the Parallelscavenge Collector's collection strategy is directly Major GC selection process). Major GC is typically 10 times times slower than the minor GC.

On Java memory allocation and recovery strategy

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.