JVM tuning-New Generation and jvm tuning New Generation
A new generation of Java virtual machines, including eden space and two dedicated vor spaces.
The new generation is used to store newly created objects. The new generation features fast object updates and produces a large number of "Dead objects" in a short time ". Garbage collection for young generations is called secondary garbage collection (minor gc)
1. New Generation and secondary recovery
The new generation is divided into three regions: One eden spac and two same-sized zoovor. The application can only use one eden and one zoovor. When primary garbage collection occurs, the gc suspends the program, then, copy the surviving objects in eden and elastic Vora to another inactive elastic vorb, clear eden and elastic vor at one time, and mark the original elastic vorb as invalid VOA. move the objects that still exist after the specified number of recycles to the old age. After secondary recycles, an empty available eden is obtained.
JVM optimization mainly focuses on memory management optimization, including controlling the size of each generation and GC policy. The application thread is suspended when GC starts garbage collection, which seriously affects the performance. The purpose of optimization is to minimize the pause time of the Application Thread caused by GC and reduce the number of Full GC operations.
2. Optimization of the New Generation
Key parameters of the new generation:-Xms,-Xmx,-Xmn,-XX: Invalid vorratio,-XX: MaxTenuringThreshold,-XX: PermSize,-XX: MaxPermSize
-Xms and-Xmx are usually set to the same value to avoid continuous JVM memory expansion during runtime. This value determines the maximum memory that JVM heap can use.
-Xmn determines the size of the new generation space. The ratio of the New Generation Eden, S0, and S1 can be controlled through-XX: discounted vorratio (if the value is 4, Eden: S0: s1 =)
-XX: the number of times the MaxTenuringThreshold control object enters the old age after the minor GC. this parameter is only valid for Serial GC.
-XX: PermSize,-XX: MaxPermSize is used to control the size of the Method Area, usually set to the same value.
1. Avoid setting too small for the new generation.
When the young generation is set to an hour, there are two obvious phenomena: frequent minor GC times and direct minor GC objects to the old generation. When the memory in the old age is insufficient, Full GC is triggered.
2. Avoid setting too large for the new generation.
When the new generation is too large, there are two problems: first, the old generation gets smaller, which may lead to frequent Full GC execution; second, the minor GC execution time increases significantly.
3. Avoid too large or too small vor zone.
-XX: the greater the value of the region vorratio parameter, it means that the Eden region is larger, the number of minor GC operations is reduced, but the two region vor regions become smaller, if the objects that exceed the memory size of the region vor are not recycled after the minor GC, they will directly enter the old age;
-XX: if the value of the interval vorratio parameter is set to be too small, it means that the Eden region becomes smaller, the number of minor GC triggers increases, and the interval vor region increases. This means that more objects can be stored after the minor GC, avoid entering the old age.
4. Reasonably set the lifetime cycle of the object in the new generation.
The survival cycle of the new generation determines how many times the new generation object enters the old age after Minor GC. Therefore, this value must be optimized based on your application. The Jvm parameter corresponds to-XX: MaxTenuringThreshold, which is 15 times by default.
3. view the source code of two swap vor Spaces
Source code for reference only
Package com. gc;
Import java. util. ArrayList;
Import java. util. Random;
/**
* For simple applications, you can see the volume vor space switch, and view it with the jstat that comes with JDK.
* Parameter:-Xms30m-Xmx60m
*
* @ Author fan fangming
*/
Public class easyvariables vorchange {
Public byte [] placeHolder = new byte [1*1024]; // placeHolder
Public static void main (String [] args) throws Exception {
While (true ){
Random random = new Random ();
Int loops = random. nextInt (10000 );
Easybench vorchangejstat = new easybench vorchange ();
System. out. println ("... building object:" + loops + "(items )");
Jstat. getLoop (loops); // multiple loops generate a large number of objects
Thread. sleep (10 );
}
}
Public void getLoop (int size ){
ArrayList <easyroute vorchange> list = new ArrayList <easyroute vorchange> ();
For (int I = 0; I <size; I ++ ){
Easybench vorchangejstat = new easybench vorchange ();
List. add (jstat );
}
}
}
4. Use jstat to view swap vor space exchanges
C: \ Program Files \ Java \ jdk1.6.0 _ 25 \ bin> jps
Jps 8068
2384
7248 easybench vorchange
C: \ Program Files \ Java \ jdk1.6.0 _ 25 \ bin> jstat-gc 7248 250 6
S0C S1C S0U S1U EC EU
128.0 64.0 0.0 63.8 1024.0 0.0
128.0 128.0 0.0 127.6 1152.0 0.0
64.0 64.0 63.6 0.0 896.0 0.0
64.0 64.0 0.0 64.0 896.0 0.0
64.0 64.0 0.0 63.8 896.0 0.0
64.0 64.0 0.0 0.0 896.0 0.0
S0C indicates the current S0 capacity (KB), S1C indicates the current S1 capacity (KB), and S0U indicates the size (KB) used by the current S0 ), s1U indicates the size (KB) of the current S1 ). we know that the data in the two vor zones is marked for exchange. here we can see that the space usage of S0U and S1U is alternating.