Tomcat configuration and optimization (memory, concurrency, management)

Original link: http://blog.csdn.net/xyang81/article/details/51530979

1. JVM memory configuration optimization

In development, when a project is relatively large, there are usually many dependent jar packages. We all know that when the application server starts, all classes referenced by the application will be loaded into memory in turn through ClassLoader. Java's logical memory model is roughly divided into heap memory, stack memory, and static memory area, also known as persistent area. The memory in this area will not be reclaimed by GC. The heap memory is used to store class instances, arrays and other reference type data, that is, objects generated by new are stored here, the stack memory stores local variables (such as method parameters), and the static memory area stores constants, static variables, classes Metadata information (methods, properties, etc.). There are three types of memory overflow exceptions often encountered in development:

  • java.lang.OutOfMemoryError: The Java heap space exception 
    indicates that the heap memory space is full. If it is not a bug in the program logic, it may be because there are many jars referenced in the project, which leads to memory overflow. The minimum used memory of the JVM default heap is 1/64 of the physical memory, and the maximum used memory is 1/4 of the physical memory. For example, the physical memory of 8G, the minimum and maximum memory of the JVM default heap are 128m and 2048m respectively. Increase the memory usage limit by adjusting the JVM's -Xms (initial memory) and -Xmx (maximum memory) parameters.
  • java.lang.OutOfMemoryError: PermGen space exception 
    indicates that the static memory area is full, usually caused by too many classes loaded. Versions below jdk8 limit the minimum and maximum memory ranges of the static area by modifying the JVM's -XX:PermSize and -XX:MaxPermSize parameters. jdk8 changed the memory model and stored the class definition in the metadata (MetaspaceSize) space, and the metadata space shares the same memory area with the heap space, so there will be no PermGen space exception after JDK8, so there is no need to set this parameter.
  • The java.lang.StackOverflowError exception 
    indicates stack overflow. Usually caused by infinite loop, infinite recursion.

Modify the memory configuration of Tomcat, open the $TOMCAT_HOME/bin/catalina.sh file (the Windows system is the catalina.bat file), about line 250, and add the memory parameter settings to the JAVA_OPTS parameter. The complete JVM parameter setting looks like this:

JAVA_OPTS="$JAVA_OPTS -server -Xms2048m -Xmx2048m -XX:PermSize=128m -XX:MaxPermSize=256 -Djava.awt.headless=true"

-server parameter: Indicates to start in service mode, the startup speed will be slightly slower, but the performance will be much higher. Without this parameter, the default is to start in client mode. 
java.awt.headless=true parameter: related to graphics operation, suitable for linux system. If a verification code is generated, it means that the server is currently using a server that does not have an installation diagram to install a graphical interface. If the system displays relevant parameters in the application, an exception will be thrown. You can use jmap -heap proccess_id to check whether the setting is successful. 
JAVA_OPTS parameter setting

2. Concurrent configuration optimization

Mainly configure the number of requests that Tomcat can handle. When the number of threads in a process exceeds 500, the running efficiency of the process is very low. On the surface, the more threads, the more requests processed. In fact, too many threads will occupy the resources of the CPU to switch between different threads, resulting in a limited time slice for the CPU to process on each thread, which will reduce the response of the server. performance.

<Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
               connectionTimeout="20000"
               redirectPort="8443" 

                maxThreads="500"
                minSpareThreads="100"
                maxSpareThreads="200"
                acceptCount="200"
                maxIdleTime="30000"
                enableLookups="false"
               />

The number of concurrent requests processed by Tomcat = maxThreads + acceptCount

protocol: Enable APR connection mode to improve asynchronous IO processing performance. For enabling configuration, please refer to: "Enable Tomcat APR Running Mode to Optimize Concurrent Performance" 
maxThreads: The maximum number of requests that can be accepted, the default is 200 
minSpareThreads: The minimum number of spare threads, the default initialization, the default is 25 
maxSpareThreads: The maximum number of spare threads, once created If the number of threads exceeds this value, Tomcat will close the socket threads that are no longer needed. 
acceptCount: The request queue waiting to be processed, the default is 100. If the queue length exceeds the length, the server will reject the client request and return 403 directly 
. maxIdleTime: If a thread is in 30 seconds If there is no activity within, terminate the operation and remove it from the thread pool. Unless the number of thread pools is less than or equal to the number of minSpareThreads. The default value is 1 minute 
enableLookups: if it is true, calling request.getRemoteHost will perform a DNS reverse lookup, and reversely resolve the domain name or host corresponding to the IP, which is inefficient. It is recommended to set it to false. 
For more parameter settings, please refer to the official Tomcat documentation: http://tomcat.apache.org/tomcat-8.0-doc/config/http.html

3. Administrator configuration

Tomcat does not have the privilege to configure the administrator account by default. If you want to view the deployment status of the app, deploy or undeploy through the management interface, you need to configure a user with administrative privileges to log in in tomcat-user.xml.

<role rolename="tomcat"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<user username="tomcat" password="tomcat" roles="tomcat,manager-gui,manager-status,manager-script,manager-jmx"/>

Tomcat official website configuration: http://tomcat.apache.org/tomcat-8.0-doc/manager-howto.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325402746&siteId=291194637