Configuration and optimization of Tomcat

Source: Internet
Author: User
Tags app service xms

Tomcat's memory usage configuration, maximum number of connections configured. How to modify the configuration, under the/tomcat/bin/there is a script file catailna.sh. If Windows is bat, setting up Tomcat's use of memory is actually setting the JVM's usage parameters.

one. Tomcat Memory optimization

Tomcat memory optimization is primarily optimized for Tomcat boot parameters, and we can set the Java_opts parameter in Tomcat's startup script catalina.sh.

1.java_opts parameter Description

Java code

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

Sets the initial memory for Tomcat startup, where the initial space (that is,-XMS) is 1/64 of physical memory, and the maximum space (-XMX) is 1/4 of physical memory. You can take advantage of options such as the-XMN-XMS-XMX provided by the JVM, and the "M" description is MB, otherwise it is KB and will be reported out of memory when starting Tomcat.

 -Xms:初始值  【初始化内存大小】
-Xmx:最大值  【可以使用的最大内存】
-Xmn:最小值

Tip: This exception message is thrown in the JVM if 98% of the time is used for GC and the available heap size is less than 2%.

Tip: The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4. The size of these two values is generally set as needed. The size of the initialized heap performs the amount of memory requested by the virtual machine to the system at startup. Generally speaking, this parameter is not important. However, some applications in the case of heavy load will take up more memory, when this parameter is very important, if the virtual machine is set up when the memory is relatively small and in this case there are many objects to initialize, the virtual machine must repeatedly increase the memory to meet the use. For this reason, we generally set-XMS and-xmx as large, and the maximum value of the heap is limited by the physical memory used by the system. Applications that use large amounts of data generally use persistent objects, and memory usage can grow rapidly. The virtual machine prompts for memory overflow when the application needs more memory than the heap, and causes the app service to crash. Therefore, it is generally recommended that the maximum heap value be set to 80% of the maximum available memory.

    • If your system spends a lot of time collecting garbage, reduce the heap size. A complete garbage collection should be no more than 3-5 seconds. If garbage collection becomes a bottleneck, you need to specify the size of the generation, check the detailed output of the garbage collection, and investigate the performance impact of garbage collection parameters. Generally speaking, you should use 80% of the physical memory as the heap size. When adding processors, remember to increase the memory because allocations can be done in parallel, and garbage collection is not parallel.

    • These configuration changes will not be valid until you restart your Tomcat server.

Windows adds the following settings to the file {Tomcathome}/bin/catalina.bat,unix in front of the file {tomcathome}/bin/catalina.sh:

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

Java_opts parameters

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, you can restart Tomcat to see if the configuration takes effect with the following command:

1. First look at the Tomcat process number:

ps -ef | grep tomcat

We can see that the Tomcat process number is 9217.

1. See if the configuration takes effect:

sudo jmap –heap 9217

We can see that the parameters such as maxheapsize have been in effect.

two. Tomcat Concurrency optimization

1.Tomcat Connection Related parameters

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

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

1. Parameter description
The default Tomcat parameters:

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

Modify:

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

After this setup, there is basically no more machine.

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"//指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理

This is an optimization of the HTTP connector, and if you use Apache and Tomcat to load balance the cluster and use the AJP protocol for Apache and Tomcat protocol forwarding, you also need to optimize the AJP connector.

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

Address some of the frequently encountered errors

One, Tomcat's JVM Prompts for memory overflow

See if the log file has a memory overflow error under the%tomcat_home%\logs folder

Second, modify the Tomcat JVM

1. Error tip: Java.lang.OutOfMemoryError:Java heap Space

Tomcat can use 128MB of memory by default, and in larger applications, this memory is not enough and may cause the system to fail to run. A common problem is to report a Tomcat memory overflow error, an out of memory (system-out-of-RAM) exception, which causes the client to display a 500 error, and generally adjusts Tomcat's use of memory to resolve the issue.

Modify in Windows environment

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

Linux Environment modification

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

Where-XMS sets the initial memory size,-XMX sets the maximum memory that can be used.

You can set it up with me.

2. Error tip: Java.lang.OutOfMemoryError:PermGen space

Reason:

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)那么就会产生此错误信息了。

Workaround:

The first line in Catalina.bat is incremented:

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

The first line in catalina.sh is incremented:

JAVA_OPTS=-Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSize=256m  XX:MaxPermSize=256m
third, view Tomcat's JVM memory
    1. No default user is set in Tomcat6, so users need to be manually added to the Tomcat-users.xml file under Tomcat6 conf folder. Such as:

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

Note: You need to restart TOMCAT6 after adding.

    1. To access Http://localhost:8080/manager/status, enter the user name and password you added above.

    2. The memory usage can then be seen under the JVM below.

Original link

Configuration and optimization of Tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.