tomcat 优化实记
好记性不如烂笔头,以下是 tomcat 无脑优化
1.内存设置(VM参数调优)
(1). Windows环境下,是tomcat解压版(执行startup.bat启动tomcat)
解决办法:
修改“%TOMCAT_HOME%\bin\catalina.bat”文件,
在文件开头增加如下设置:
1 set JAVA_OPTS=-Xms512m -Xmx512m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=512m
备注:一定加在catalina.bat最前面。
(2).Linux环境下
解决办法:
修改“%TOMCAT_HOME%\bin\catalina.sh”文件,
在文件开头增加如下设置:
JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"
注意:前后二者区别,有无set,有无双引号。
各参数详解:
-Xms:设置JVM初始内存大小(默认是物理内存的1/64)
-Xmx:设置JVM可以使用的最大内存(默认是物理内存的1/4,建议:物理内存80%)
-Xmn:设置JVM最小内存(128-256m就够了,一般不设置)
默认空余堆内存小于 40%时,JVM就会增大堆直到-Xmx的最大限制;空余堆内存大于70%时,JVM会减少堆直到-Xms的最小限制。因此服务器一般设置-Xms、 -Xmx相等以避免在每次GC 后调整堆的大小。
在较大型的应用项目中,默认的内存是不够的,有可能导致系统无法运行。
常见的问题是报Tomcat内存溢出错误
“java.lang.OutOfMemoryError: Java heap space”,
从而导致客户端显示500错误。
-XX:PermSize :为JVM启动时Perm的内存大小
-XX:MaxPermSize :为最大可占用的Perm内存大小(默认为32M)
-XX:MaxNewSize,默认为16M
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被放到PermGen space中,它和存放类实例(Instance)的Heap区域不同,GC(Garbage Collection)不会在主程序运行期对PermGen space进行清理,
所以如果你的应用中有很CLASS的话,
就很可能出现“java.lang.OutOfMemoryError: PermGen space”错误。
对于WEB项目,jvm加载类时,永久域中的对象急剧增加,从而使jvm不断调整永久域大小,为了避免调整),你可以使用更多的参数配置。如果你的WEB APP下都用了大量的第三方jar,
其大小超过了jvm默认的大小,那么就会产生此错误信息了。
其它参数:
-XX:NewSize :默认为2M,此值设大可调大新对象区,减少Full GC次数
-XX:NewRatio :改变新旧空间的比例,意思是新空间的尺寸是旧空间的1/8(默认为8)
-XX:SurvivorRatio :改变Eden对象空间和残存空间的尺寸比例,意思是Eden对象空间的尺寸比残存空间大survivorRatio+2倍(缺省值是10)
-XX:userParNewGC 可用来设置并行收集【多CPU】
-XX:ParallelGCThreads 可用来增加并行度【多CPU】
-XXUseParallelGC 设置后可以使用并行清除收集器【多CPU】
2.修改tomcat让其支持NIO
修改前:
<Connector port="9091" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443" />
修改成支持NIO的类型,配置如下 :
<Connector port="9091" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" />
3.并发数设置
默认的tomcat配置,并发测试时,可能30个USER上去就当机了。
添加
maxThreads="600" //最大线程数
minSpareThreads="100" //初始化时创建的线程数
maxSpareThreads="500" //一旦线程超过这个值,Tomcat会关闭不需要的socket线程
acceptCount="700"//指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理
connectionTimeout="20000"
redirectPort="8443" />
或者
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="70" maxSpareThreads="500" acceptCount="700" maxIdleTime="60000" />
<Connector executor="tomcatThreadPool" port="9091" protocol="HTTP/1.1" connectionTimeout="60000" keepAliveTimeout="15000" maxKeepAliveRequests="1" redirectPort="8443" />
如果你的是使用 AJP 协议,集成的集群那么你还需要
<Connector port="13000" protocol="AJP/1.3" executor="tomcatThreadPool"
connectionTimeout="30000" redirectPort="8443" />
这里最好使用这种方式,让2个端口使用同一个线程池
4.Java虚拟机调优
应该选择SUN的JVM,在满足项目需要的前提下,尽量选用版本较高的JVM,一般来说高版本产品在速度和效率上比低版本会有改进。 JDK1.4比JDK1.3性能提高了近10%-20%,JDK1.5比JDK1.4性能提高25%-75%。
5.禁用DNS查询
server.xml 设置enableLookups="false":
enableLookups="false" redirectPort="8443" URIEncoding="UTF-8" acceptCount="1000" />
当web应用程序向要记录客户端的信息时,它也会记录客户端的IP地址或者通过域名服务器查找机器名转换为IP地址。DNS查询需要占用网络, 并且包括可能从很多很远的服务器或者不起作用的服务器上去获取对应的IP的过程,这样会消耗一定的时间。为了消除DNS查询对性能的影响我们可以关闭 DNS查询,
方式是修改server.xml文件中的enableLookups参数值为false。
6.设置解决乱码问题
URIEncoding="UTF-8" />
Tomacat7启动报错如下(Jar包冲突):
解决方法为:在Tomacat7的context.xml文件里的<Context>中加上(使用本地Jar包)
java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter
<Loader delegate="true" />
我的tomcat 配置conf/server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="9001" shutdown="SHUTDOWN">
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="70" maxSpareThreads="500" acceptCount="700" maxIdleTime="60000" /> <!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080 <Connector port="9091" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /> <Connector port="9091" protocol="HTTP/1.1" maxThreads="700" minSpareThreads="90" maxSpareThreads="500" acceptCount="900"
connectionTimeout="30000" redirectPort="8443" />
-->
<Connector executor="tomcatThreadPool" port="9091" protocol="HTTP/1.1" connectionTimeout="60000" keepAliveTimeout="15000" maxKeepAliveRequests="1" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
--> <!-- Define an AJP 1.3 Connector on port 8009
<Connector port="11000" protocol="AJP/1.3" redirectPort="8443" />--> <Connector port="11000" protocol="AJP/1.3" executor="tomcatThreadPool"
connectionTimeout="30000" redirectPort="8443" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html --> <!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3"> <!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation)
-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> <!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8" >
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true" /> <Channel className="org.apache.catalina.tribes.group.GroupChannel" >
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000" />
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000" selectorTimeout="5000"
maxThreads="6" /> -->
<!-- timeout="60000"
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter" >
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />
</Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="" />
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" /> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false" /> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" /> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />
</Cluster>
--> <!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm> <Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="E:\Clusters\static\" reloadable="true" crossContext="true"/>
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
--> <!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" /> </Host>
</Engine>
</Service>
</Server>
tomcat 优化实记的更多相关文章
- tomcat优化
tomcat优化:vim catalina.sh添加:JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -X ...
- windows tomcat 优化
windows tomcat 优化 1. tomcat conf server.xml 在server.xml中修改以一部分,增加节点数目,可以很好的提高性能: <Connector port ...
- Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法
Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法 java.lang.OutOfMemoryError: Java heap sp ...
- tomcat优化-有改protocol 和 缓存 集群方案
tomcat优化 在线上环境中我们是采用了tomcat作为Web服务器,它的处理性能直接关系到用户体验,在平时的工作和学习中,归纳出以下七种调优经验. 1. 服务器资源 服务器所能提供CPU.内存.硬 ...
- tomcat 优化配置 java-8 tomcat-7
tomcat 优化配置 , 说明 一.并发优化 1.JVM调优 以下为1G物理内存tomcat配置: JAVA_OPTS="-server -Xms512M -Xmx512M -Xss256 ...
- Tomcat 优化方案 和 配置详解(转)
转自 Tomcat 优化方案 和 配置详解 http://201605130349.iteye.com/blog/2298985 Server.xml配置文件用于对整个容器进行相关的配置. <S ...
- Tomcat优化之容易集合经验
Tomcat优化1. 如何加大tomcat连接数在tomcat配置文件server.xml中的<Connector ... />配置中,和连接数相关的参数有: maxThreads : t ...
- tomcat优化实例
———————————————————————————————————— 一.运行模式优化 修改tomcat运行模式为nio<Connector port="80" prot ...
- tomcat优化之安装并配置apr库
在谈到tomcat优化时,必然要说到apr库,这个库是C语言实现的,tomcat通过JNI方式使用该库可以大大提高性能. tomcat在使用apr时需要安装apr,apr-util和tomcat-na ...
随机推荐
- Sql Server Row_Number() 学习
Row_Number(): row_number()主要是为选出的每一条记录按照一定的排序方式生成一个行序号. 语法: ROW_NUMBER ( ) OVER ( [ PARTITION BY val ...
- cocos2dx-lua之断点调试支持
cocos2dx 3.2版对cocos code ide支持已经相当棒了,不过话说,编辑器用起来感觉没有sublime顺手 支持cocos code ide已经支持创建lua项目了,可是默认创建的项目 ...
- AMQ学习笔记 - 06. 可靠消息传送
概述 本文介绍JMS中可能发生消息故障的3个隐患阶段,以及确保消息安全的3种保障机制. 故障分析 在介绍可靠传送的确保机制之前,先分析消息在传送的过程中可能在哪个阶段出现问题. 1.两个跃点 跃点的含 ...
- SpringMvc入门二----HelloWorld
1. 导入需要的架包: 2. 配置web.xml,添加Servlet <servlet> <servlet-name>springmvc</servlet-name> ...
- mplayer-for-windows change color scheme in win 7
Q: When I play movie on Windows7, always comes this message: The color scheme has been changed The f ...
- BCB6中SCALERICHVIEW加入GIF动画
记载下,花了不少时间. 1. 项目导入文件GIFImage.pas 来源:http://melander.dk/delphi/gifimage/ 2. 项目导入文件RVGifAnimate.pas ...
- JQGrid+Webservice+LINQ
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jqgridtest.asp ...
- margin的BUG
在进行简单的div盒子嵌套时,发现设置margin-top时存在bug,然后就去谷歌搜索了一下,发现margin确实存在bug. bug的现象是父子元素嵌套时,如果子元素是块元素时,对块元素设置mar ...
- 为什么要用ajax
Ajax应用程序的优势在于:1. 通过异步模式,提升了用户体验2. 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用3. Ajax引擎在客户端运行,承担了一部分本来由服务器承担的工 ...
- PHP中include和require绝对路径、相对路径问题
在写PHP程序时,经常要用到include或require包含其他文件,但是各文件里包含的文件多了之后,就会产生路径问题. 如下目录: <web>(网站根目录) ├<A>文件夹 ...