您的位置:首页 > 运维架构 > Tomcat

Tomcat7以及以上版本设置最大连接数和内存

2017-12-01 11:29 561 查看
一、Tomcat 最大连接数设置:

Tomcat的官网介绍:

If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the maxThreads attribute). If still more simultaneous requests
are received, they are stacked up inside the server socket created by the Connector, up to the configured maximum (the value of the acceptCount attribute).

所以我们需要设置的是maxThreads和acceptCount这两个值:

其中,maxThreads的介绍如下:

The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this
connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool.

而acceptCount的介绍为:

The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.

所以两者的默认值分别是200和100,要调整Tomcat的默认最大连接数,可以增加这两个属性的值,并且使acceptCount大于等于maxThreads:

[html] view
plaincopyprint?

<Connector port="8080" protocol="HTTP/1.1"   

           connectionTimeout="20000"   

           redirectPort="8443" acceptCount="500" maxThreads="400" />  

Html代码  


<Connector port="8080" protocol="HTTP/1.1"   

           connectionTimeout="20000"   

           redirectPort="8443" acceptCount="500" maxThreads="400" />  

二、Tomcat7以及以上版本内存设置

Tomcat的启动分为startupo.bat启动和注册为windows服务的启动,下面一一说明。
1.startup.bat启动
在tomcat_home/bin目录下找到catalina.bat,用文本编辑器打开,加上下面一行:
set JAVA_OPTS= -Xms1024M -Xmx1024M -XX:PermSize=256M -XX:MaxNewSize=256M -XX:MaxPermSize=256M
解释一下各个参数:
-Xms1024M:初始化堆内存大小(注意,不加M的话单位是KB)
-Xmx1029M:最大堆内存大小
-XX:PermSize=256M:初始化类加载内存池大小
-XX:MaxPermSize=256M:最大类加载内存池大小
-XX:MaxNewSize=256M:这个还不清楚哈,有知道的说声
还有一个-server参数,是指启动jvm时以服务器方式启动,比客户端启动慢,但性能较好,大家可以自己选择。
2.windows服务启动
       如果你的tomcat是注册为windows服务并且是以服务方式启动的,那么上面的方法就无效了,因为这时tomcat启动是读取注册表的参数,而不是读取批处理文件的参数,这时我们有两种方法来设置jvm参数。
第一种比较简单,tomcat为我们提供了一个设置启动参数的窗体,双击tomcat_home/bin目录下的tomcat6w.exe,如图



下方的Initial memory pool就是初始化堆内存大小,Maximun memory pool是最大堆内存大小。
而要设置Perm Gen池的大小就要在Java Option里面加参数了,在里面加上:
-Dcatalina.base=%tomcat_home%
-Dcatalina.home=%tomcat_home%
-Djava.endorsed.dirs=%tomcat_home%\endorsed
-Djava.io.tmpdir=%tomcat_home%\temp
-XX:PermSize=256M
-XX:MaxPermSize=256M
-XX:ReservedCodeCacheSize=48M
-Duser.timezone=GMT+08
(PS:网上说每一行后面不要有空格,没试过)
第二种方法是打开注册表->HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat6\Parameters\Java(路径可能有一点点差别)



修改Options的值,把刚才上面那些参数加进去就OK了。(别忘了先备份一下注册表)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: