Configuration and optimization of tomcat

The memory usage configuration of tomcat and the configuration of the maximum number of connections.

 

How to modify the configuration? There is a script file catailna.sh under /bin/ of /tomcat. If Windows is bat to set the memory usage of tomcat, it is actually setting the usage parameters of jvm.

 

1. Tomcat memory optimization

 

Tomcat memory optimization is mainly to optimize the tomcat startup parameters, we can set the JAVA_OPTS parameter in the tomcat startup script catalina.sh.

 

1. JAVA_OPTS parameter description

 

Java code

 -server  启用jdk 的 server 版;
 -Xms      java虚拟机初始化时的最小内存;
 -Xmx      java虚拟机可使用的最大内存;
 -XX:PermSize    内存永久保留区域
 -XX:MaxPermSize   内存最大永久保留区域

Set the initial memory for Tomcat to start, its initial space (ie -Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) is 1/4 of the physical memory. You can use the options such as -Xmn -Xms -Xmx provided by the JVM. Add "m" to indicate that it is MB, otherwise it is KB, and it will report insufficient memory when starting tomcat.

 

 -Xms:初始值  【初始化内存大小】
-Xmx:最大值  【可以使用的最大内存】
-Xmn:最小值
JVM堆的设置是指java程序运行过程中JVM可以调配使用的内存空间的设置.JVM在启动的时候会自动设置Heap size的值,其初始空间(即-Xms)是物理内存的1/64,最大空间(-Xmx)是物理内存的1/4。可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置。Heap size 的大小是Young Generation 和Tenured Generaion 之和。

 

Hint: This exception will be thrown if 98% of the time in the JVM is used for GC and the available Heap size is less than 2%.

 

Tip: The maximum Heap Size should not exceed 80% of the available physical memory. Generally, the -Xms and -Xmx options should be set to the same, and -Xmn is 1/4 of the -Xmx value. The size of these two values ​​is generally set as required. The size of the initialization heap implements the size of the memory that the virtual machine requests from the system at startup. In general, this parameter is not important. However, some applications will rapidly occupy more memory under heavy load. At this time, this parameter is very important. If the memory used when the virtual machine is started is relatively small and there are many objects in this case. Initialization, the virtual machine must repeatedly increase the memory to meet the use. For this reason, we generally set -Xms and -Xmx to be the same size, and the maximum heap size is limited by the physical memory used by the system. Generally, applications that use large amounts of data use persistent objects, and memory usage is likely to grow rapidly. When the memory required by the application exceeds the maximum heap size, the virtual machine will prompt a memory overflow and cause the application service to crash. Therefore, it is generally recommended that the maximum heap size be set to 80% of the maximum available memory.

  • If the system spends a lot of time collecting garbage, reduce the heap size. A full garbage collection should take no more than 3-5 seconds. If garbage collection becomes the bottleneck, then specify the generation size, examine the detailed output of garbage collection, and study the effect of garbage collection parameters on performance. In general, you should use 80% of physical memory as the heap size. When adding processors, remember to increase memory because allocation can be done in parallel, while garbage collection is not.

  • These configuration changes will not take effect until you restart your Tomcat server.

Windows is in the file {tomcathome}/bin/catalina.bat, Unix is ​​in front of the file {tomcathome}/bin/catalina.sh, add the following settings:

Server parameter configuration
tomcat默认: -Xms1024m -Xmx1024m -Xss1024K -XX:PermSize=128m -XX:MaxPermSize=256m

Java_OPTS parameter

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8
-server -Xms2048m -Xmx2048m
-XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m
-XX:MaxPermSize=512m -XX:+DisableExplicitGC"

After the configuration is complete, restart Tomcat, and run the following command to check whether the configuration takes effect:

 

1. First check the Tomcat process number:

ps -ef | grep tomcat

We can see that the Tomcat process number is 9217 

 

1. Check whether the configuration takes effect:

sudo jmap –heap 9217

We can see that parameters such as MaxHeapSize have taken effect.

 

2. Tomcat concurrency optimization

 

1. Tomcat connection related parameters

 

The parameters related to the number of connections in the configuration in server.xml under the Tomcat configuration file conf are:

 

minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为10
maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为75
acceptCount:允许的最大连接数,应大于等于maxProcessors,默认值为100
enableLookups:是否反查域名,取值为:true或false。为了提高处理能力,应设置为false
connectionTimeout:网络连接超时,单位:毫秒。设置为0表示永不超时,这样设置有隐患的。通常可设置为30000毫秒。

1. Parameter description 
Default tomcat parameters:

<Connector port=“8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

Revise:

<Connector port=“8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
  maxThreads="600"
  minSpareThreads="100"
  maxSpareThreads="500"
  acceptCount="700"
  connectionTimeout="20000"
  redirectPort="8443" />

After this setting, there is basically no crash.

protocol="org.apache.coyote.http11.Http11NioProtocol" ///使用java的异步io护理技术,no blocking IO
maxThreads=“600" 表示最多同时处理600个连接 ///最大线程数
minSpareThreads=“100" 表示即使没有人使用也开这么多空线程等待  ///初始化时创建的线程数
maxSpareThreads=“500" 表示如果最多可以空500个线程,例如某时刻有505人访问,之后没有人访问了,则tomcat不会保留505个空线程,而是关闭505个空的。   ///一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程。
acceptCount="700"//指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理

Here is the optimization of the http connector. If apache and tomcat are used for cluster load balancing, and the ajp protocol is used for protocol forwarding between apache and tomcat, then the ajp connector needs to be optimized.

<Connector port="8009" protocol="AJP/1.3" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700" connectionTimeout="20000" redirectPort="8443" />

 

Solve some common errors

1. Tomcat's JVM prompts memory overflow

Check the %TOMCAT_HOME%\logs folder to see if there is a memory overflow error in the log file

 

2. Modify Tomcat's JVM

1. Error message: java.lang.OutOfMemoryError: Java heap space

Tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,有可能导致系统无法运行。常见的问题是报Tomcat内存溢出错误,Out of Memory(系统内存不足)的异常,从而导致客户端显示500错误,一般调整Tomcat的使用内存即可解决此问题。

windows环境下修改

“%TOMCAT_HOME%\bin\catalina.bat”文件,在文件开头增加如下设置:JAVA_OPTS=-Xms2048m -Xmx2048m

Linux环境下修改

“%TOMCAT_HOME%\bin\catalina.sh”文件,在文件开头增加如下设置:JAVA_OPTS=-Xms2048m -Xmx2048m

其中,-Xms设置初始化内存大小,-Xmx设置可以使用的最大内存。

跟我上面那么设置就可以了。

 

2、错误提示:java.lang.OutOfMemoryError: PermGen space

原因:

PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存
放Class和Meta信息的,Class在被Loader时就会被放到PermGen space中,它和存放类实例(Instance)的Heap区域不同,GC(Garbage Collection)不会在主程序运行期对PermGen space进行清理,所以如果你的应用中有很CLASS的话,就很可能出现PermGen space错误,这种错误常见在web服务器对JSP进行pre compile的时候。如果你的WEB APP下都用了大量的第三方jar, 其大小超过了jvm默认的大小(4M)那么就会产生此错误信息了。

解决方法:

在catalina.bat的第一行增加:

set JAVA_OPTS=-Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSize=256m - XX:MaxPermSize=256m

在catalina.sh的第一行增加:

JAVA_OPTS=-Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSize=256m  XX:MaxPermSize=256m
三、查看Tomcat的JVM内存
  1. Tomcat6中没有设置任何默认用户,因而需要手动往Tomcat6的conf文件夹下的tomcat-users.xml文件中添加用户。 如:

       <role rolename="manager"/>
      <user username="tomcat" password="tomcat" roles="manager"/>

注:添加完需要重启Tomcat6。

2.tomcat7

   <role rolename="manager"/>
  <user username="admin" password="" roles="manager-gui"/>
 
  1. 访问http://localhost:8080/manager/status,输入上面添加的用户名和密码。

  2. 然后在如下面的JVM下可以看到内存的使用情况。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676651&siteId=291194637