Home  >  Article  >  Java  >  JVM parameter tuning example analysis

JVM parameter tuning example analysis

巴扎黑
巴扎黑Original
2017-06-27 09:19:481295browse

Original source:

Regarding JVM parameter tuning, it is a headache for many programmers. If the settings are not good, the JVM will continue to execute Full GC, which will As a result, the entire system becomes very slow, and the website stagnation time can reach more than 10 seconds. If this happens every few minutes, I will not be able to stand it.

This kind of stagnation cannot be seen during testing. The problem is exposed only when the website pv reaches hundreds of thousands/day. In order to configure the JVM parameters properly, you need to understand the young generation and the younger generation. You have a certain understanding of the old generation, rescue space and permanent generation. You also need to understand the JVM memory management logic, and ultimately make adjustments according to your own application. You can find a lot of JVM parameters by searching on the Internet. There are also many practical examples. I have also tested various examples, but problems will eventually occur. After several months of practice and improvement, I came up with the website (no requirements required). The following experiences are given for tuning jvm parameters such as dead time.

1: It is recommended to use a 64-bit operating system. The 64-bit JDK under Linux is slower than the 32-bit JDK, but it consumes more memory and has greater throughput.

2: The XMX and XMS settings are the same size, and the MaxPermSize and MinPermSize settings are the same size, which can reduce the pressure caused by the scaling heap size.

3: Set some printing parameters when debugging, such as -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:log/gc.log, so that you can start from gc Some clues can be seen in the .log.

4: When the system pauses, it may be a GC problem or a program problem. Use Jmap and Jstack to check, or killall -3 Java, and then check the Java console log, you can see many problems. Once, the website suddenly became very slow. When Jstack took a look, it turned out that there were too many URL Connections written by himself and were not released. He changed the program and it was OK.

5: Carefully understand your application. If you use caching, the old generation should be larger. The cached HashMap should not be infinitely long. It is recommended to use the LRU algorithm Map for caching. The maximum length of LRUMap is also It should be set according to the actual situation.

6: Promotion Failed is a very troublesome problem during garbage collection. Generally, it may occur for two reasons. The first reason is that the rescue space is not enough, and the objects in the rescue space should not be moved to the old generation. , but there are many objects in the young generation that need to be put into the rescue space; the second reason is that the old generation does not have enough space to accept objects from the young generation; in both cases, it will turn to Full GC, and the website pause time will be longer. My final solution for the first reason is to remove the rescue space and set -XX:SurvivorRatio=65536 -XX:MaxTenuringThreshold=0. My solution for the second reason is to set CMSInitiatingOccupancyFraction to a certain value (assuming 70). In this way, CMS will be executed when the old generation space reaches 70%, and the old generation will have enough space to accept objects from the young generation.

7: No matter what, the permanent generation will gradually become full, so it is necessary to restart the Java server every now and then. I restart it automatically every day.

8: When using concurrent recycling, the young generation should be smaller and the old generation should be larger. Because the old generation uses concurrent recycling, it will not affect the continued operation of other programs and the website will not stop even if it takes a long time. , my final configuration is as follows (system 8G memory), millions of PVs every day without any problems, the website does not pause, and the website did not go down due to memory problems in 2009.

  1. ##$JAVA_ARGS .= " -Dresin.home=$SERVER_ROOT -server -Xms6000M -Xmx6000M -Xmn500M -XX:PermSize=500M
  2. ##-XX:MaxPermSize=500M -XX:SurvivorRatio=
  3. 65536 -XX:MaxTenuringThreshold=
  4. 0 -Xnoclassgc -XX:+DisableExplicitGC

  5. ##-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection

  6. ##-XX:CMSFullGCsBeforeCompaction=

    0 -XX:+CMSClassUnloadingEnabled -XX:-CMSParallelRemarkEnabled

  7. ##-XX:CMSInitiatingOccupancyFraction=
  8. 90 -XX:SoftRefLRUPolicyMSPerMB=

    0 -XX:+PrintClassHistogram

  9. ##-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:log/gc.log ";
  10. Explain that -XX:SurvivorRatio=65536 -XX:MaxTenuringThreshold=0 removes the rescue space:
  11. ◆ -Xnoclassgc disables class garbage collection, and the performance will be higher;

    ◆-XX:+DisableExplicitGC prohibits System.gc() to prevent programmers from mistakenly calling the gc method and affecting performance;

    ◆-XX:+UseParNewGC, for young people The generation uses multi-threaded parallel recycling, which makes collection faster; the CMS parameters are related to concurrent recycling.

CMSInitiatingOccupancyFraction

There is a lot of skill in setting this parameter. Basically, if (Xmx-Xmn)*(100-CMSInitiatingOccupancyFraction)/100>=Xmn is met, promotion failed will not occur. In my application, Xmx is 6000, and Concurrent garbage collection (CMS), the remaining 10% space at this time is 5500*10%=550 MB, so even if all objects in Xmn (that is, a total of 500 MB in the young generation) are moved to the old generation, 550 MB There is enough space, so as long as the above formula is met, there will be no Promotion Failed during garbage collection;

SoftRefLRUPolicyMSPerMB

I think this parameter may be useful. The official explanation is that softly reachable objects will remain alive for some amount of time after the last time they were referenced. The default value is one second of lifetime per free megabyte in the heap, I think there is no need to wait 1 second;

There are many other introductions to JVM parameters on the Internet. It is estimated that most of them have not encountered Promotion Failed, or the number of visits is too small and there is no chance to encounter it. (Xmx-Xmn)*(100-CMSInitiatingOccupancyFraction)/100>=Xmn is the formula Definitely original. If you really encounter Promotion Failed, you still have to deal with it this way.

The above is the detailed content of JVM parameter tuning example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn