2010-10-27
Write thisArticleIt mainly involves several times of Tomcat optimization some time ago, and the system has been running stably for three months. Today, I saw that the memory is healthy and the service has not been restarted automatically. The optimization parameters studied, explored, and practiced are of course not necessarily perfect. They should be determined based on the specific situation of the application. The following parameters can be added to the default Tomcat parameter Catalina. Sh to optimize JVM memory recycle: set the parameters first:
Java_opts = "-server-xms2048m-xmx2048m-xmn512m-XX: permsize = 128 M-XX: maxpermsize = 128 M-XX: newsize = 512 M-XX: maxnewsize = 512 M-XX: + useparallelgc-XX: parallelgcthreads = 2-XX: + useadaptivesizepolicy"
Details:
First, my application scenario is: if it is not optimized, the application may have the following problems: after running for a period of time, Tomcat will be inexplicably restarted or crash, and other internal performance problems.
What is the help of this JVM parameter?
I have previously learned about JVM's garbage collection mechanism in various articles. Here I just mention that JVM memory is divided into two major types: perm memory and generation memory. The perm area stores static class information. Generally, the default value is 64 mb. If your project is large, an error may be reported at startup, such as out of memory permsize,In addition, if the Spring framework is used, many classes are dynamically reflected and loaded. This exception may occur during a period of time. In this case, you can set the permsize..
Another type is the focus, and the ApplicationCodeBasically, the new class will be active in this region, and most of the JVM's work is done here. The details in this zone are very complicated. If you have time to read Sun information, here we only mention:
This zone contains the new generation and old generation regions. All new instances will be placed in the new region, while some instances that have been used for repeated recovery will be transferred to the old generation region, therefore, regional activities in the new generation are the most frequent. When the new generation has insufficient memory, it will trigger a GC in this zone-and then to the GC of the old generation-and finally it will be full GC. Full GC is costly and should be avoided as much as possible,Try to perform GC in this area of the newsize parameter. Generally, the newsize is allocated to about 1/4 of the total memory., --- In the end, if full GC is still insufficient memory, it will lead to a common out of Memory memory usage.
-XMS xmx JVM occupies the minimum and maximum physical memory
-Xmn512m: The XMS newsize and maxnewsize are both the same. Of course, my parameters are a bit repeated-xmn512m-XX: newsize = 512 M-XX: maxnewsize = 512 m the two zookeeper projects that can be removed have been used for stability and have not been adjusted. No problem. The minimum and maximum configurations must be the same, and the local JVM dynamically applies for memory loss. The same applies to other parameters.
-XX: + useparallelgc-XX: parallelgcthreads = 2-XX: + useadaptivesizepolicy
These parameters are not necessary for general applications,Useparallelgc for parallel collection, XX: number of parallel collection threads of parallelgcthreads, which is valid only when useparallelgc is configured.. Useadaptivesizepolicy allows the JVM to dynamically adapt parameters based on the situation. Of course, if you specify some parameters, the JVM will not adjust those parameters, if this parameter is added, other parameters that are not fully considered can be used by the JVM for microprocessing. In short, useparallelgc aims to speed up JVM collection.
Finally, my web application can support ip50w a day (single-host, only Apache + Tomcat simple shunting mode cluster). If it is more real-time, such as online game servers, it also needs to speed up JVM recovery. The specific requirements must be adjusted after testing based on application conditions.
Attached several related commands, available on Linux
Jmap-heap process number // query heap status
PS-Ef | grep Java // process number can be queried using this command
Jstat-gcutil-H5 14103 4S 100 // monitor GC recovery, where 14103 is the process number, which is checked every 4 seconds, 100 times