Tomcat Optimization configuration parameters

Source: Internet
Author: User
Tags garbage collection thread tomcat xms
1, memory optimization:

Optimized memory, mainly in the bin/catalina.bat/sh configuration file. On Linux, add in catalina.sh:

Java_opts= "-server-xms1g-xmx2g-xss256k-djava.awt.headless=true-dfile.encoding=utf-8-xx:maxpermsize=256m-xx: permsize=128m-xx:maxpermsize=256m "

which

-server: Enables the server version of the JDK.
-XMS: The minimum heap memory when the virtual machine is initialized.
-xmx: The maximum heap memory that a virtual machine can use. #-xms is set to the same value as-XMX, avoiding the JVM because of frequent GC-led performance fluctuations
-xx:permsize: Set non-heap memory initial value, default is 1/64 of physical memory.
-xx:maxnewsize: The Cenozoic represents the maximum value of the entire heap memory.
-xx:maxpermsize:perm (commonly known as the method area) accounts for the maximum of the entire heap memory, also known as the largest permanent reserved area of memory.

1) error message: 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, outof memory (System out-of-RAM) exception, causing the client to display 500 errors, general tuning Tomcat-XMS and-xmx to solve the problem, usually the-XMS and-xmx set to the same, The maximum value of the heap is set to 80% of the maximum amount of physical available memory.

Set java_opts=-xms512m-xmx512m

2) error message: Java.lang.OutOfMemoryError:PermGenspace

The full name of the Permgenspace is permanent generationspace, which is the permanent storage area of memory, which is primarily stored by the JVM with class and meta information, Class is placed in the permgenspace when it is loader, unlike the heap area where the class instance (Instance) is stored, the GC (garbage Collection) does not clean up permgenspace during the main program run time. So if you have a class in your application, you are likely to have PermGen space errors, which are common when the Web server precompile the JSP. If you have a large number of third-party jars under your web app that are larger than the JVM's default size (4M), this error message will be generated. Workaround:

setjava_opts=-xx:permsize=128m

3) When you use-XMS and-xmx to adjust the size of the Tomcat heap, you also need to consider the garbage collection mechanism. 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. 2. Optimization of connection number:

#优化连接数, it is primarily modified in the Conf/server.xml configuration file. 2.1, optimize the number of threads

Find connectorport= "8080" protocol= "http/1.1", add MaxThreads and Acceptcount attributes (make acceptcount greater than or equal to MaxThreads), as follows:

<connectorport= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443" acceptCount= "500" Maxthreads= "/>"

which

Maxthreads:tomcat The maximum number of threads that can be used for request processing, the default is the
number of minsparethreads:tomcat initial threads, or the minimum number of idle threads
· Maxsparethreads:tomcat the maximum number of idle threads that are exceeded will be closed
Acceptcount: When all available processing requests are used, the number of requests that can be placed in the processing queue is not processed. Default 100
2.2. Using Thread pool

Add the executor node in Server.xml, and then configure the Executor property of connector as follows:

<executorname= "Tomcatthreadpool" nameprefix= "req-exec-" maxthreads= "+" minsparethreads= "MaxIdleTime=" 60000 "/>
<connectorport=" 8080 "protocol=" http/1.1 "executor=" Tomcatthreadpool "/>

which

nameprefix: Naming prefix for thread pool threads
maxthreads: Maximum number of threads in thread pooling
minsparethreads: Minimum number of idle threads for thread pooling
maxidletime: When the minimum number of idle threads is exceeded , many threads will wait for this length of time and then close
threadpriority: Thread Priority

Note: When the tomcat concurrency user volume is large, a single JVM process may indeed open too many file handles, and the Java.net.SocketException:Too many open files error is reported. You can use the following steps to check:

ps-ef |grep Tomcat View Tomcat's process ID, record ID number, assuming the process ID is 10001
lsof-p 10001|wc-l View the current number of file operands with process ID 10001
• Use command: Ulimit- A view the maximum number of files allowed to open per user
3. Tomcat Connector Three modes of operation (BIO, NIO, APR) 3.1, three modes of comparison:

1) BIO: One thread processes a request. Disadvantage: When the concurrency is high, the number of threads is much, wasting resources. TOMCAT7 or below is used by default in Linux systems.

2) NIO: With Java's asynchronous IO processing, a large number of requests can be processed with a small number of threads. TOMCAT8 is used this way by default in Linux systems. TOMCAT7 must modify the connector configuration to start (conf/server.xml configuration file):

<connectorport= "8080" protocol= "Org.apache.coyote.http11.Http11NioProtocol" connectiontimeout= "20000" redirectport= "8443"/>

3) APR (Apache portable Runtime): Resolves IO blocking issues from the OS level. Linux supports Apr if the APR is installed and Native,tomcat is launched directly. 3.2. Apr mode

Install Apr and tomcat-native

Yum-y Install Apr apr-devel

Enter the Tomcat/bin directory, such as:

cd/opt/local/tomcat/bin/
tar xzfv tomcat-native.tar.gz
cd tomcat-native-1.1.32-src/jni/native
. Configure--with-apr=/usr/bin/apr-1-config make
&& make install

#注意最新版本的tomcat自带tomcat-native.war.gz, but its version is too high for the Yum installation, and will be configure when the error occurs.

Workaround: Yum Remove Apr apr-devel–y, uninstall yum installed Apr and apr-devel, download the latest version of Apr source package, compile the installation, or download the lower version of the tomcat-native compilation installation

After successful installation, you will also need to set environment variables for tomcat by adding 1 rows to the catalina.sh file:

Catalina_opts= "-djava.library.path=/usr/local/apr/lib"

#apr下载地址: http://apr.apache.org/download.cgi

#tomcat-native Download Address: http://tomcat.apache.org/download-native.cgi

Modify the Conf/server.xml on the 8080 end

Protocol= "Org.apache.coyote.http11.Http11AprProtocol"

<connector executor= "Tomcatthreadpool"
port= "8080"
protocol= " Org.apache.coyote.http11.Http11AprProtocol "
connectiontimeout=" 20000 "
enablelookups=" false "
Redirectport= "8443"
uriencoding= "UTF-8"/>

PS: After startup, the view log shows that the APR mode is turned on as follows

Sep 3:46:21 PM org.apache.coyote.AbstractProtocol start
info:starting protocolhandler ["http-apr-8081"]

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.