阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Tomcat参数设置,解决内存溢出问题

163次阅读
没有评论

共计 5401 个字符,预计需要花费 14 分钟才能阅读完成。

Tomcat 默认参数不适合生产环境使用,因此需要修改一些参数

1、修改启动时内存参数、并指定 JVM 时区(在 Windows Server 2008 下时间少了 8 个小时):

在 Tomcat 上运行 j2ee 项目代码时,经常会出现内存溢出的情况,解决办法是在系统参数中增加系统参数:

Windows 下,在 catalina.bat 最前面:
set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m;-Duser.timezone=GMT+08;
一定加在 catalina.bat 最前面。

Linux 下,在 catalina.sh 最前面增加:

JAVA_OPTS=”-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai”

注意:前后二者区别,有无 set,有无双引号。

2、线程池配置(Tomcat6 下)

使用线程池,用较少的线程处理较多的访问,可以提高 tomcat 处理请求的能力。使用方式:

首先。打开 /conf/server.xml,增加

<Executor name=”tomcatThreadPool” namePrefix=”catalina-exec-” 
        maxThreads=”500″ minSpareThreads=”20″ maxIdleTime=”60000″ />

最大线程 500(一般服务器足以),最小空闲线程数 20,线程最大空闲时间 60 秒。

然后,修改 <Connector …> 节点,增加 executor 属性,如:

<Connector executor=”tomcatThreadPool” 
              port=”80″

protocol=”HTTP/1.1″

maxThreads=”600″

minSpareThreads=”100″

maxSpareThreads=”300″
              connectionTimeout=”60000″
              keepAliveTimeout=”15000″
              maxKeepAliveRequests=”1″
              redirectPort=”443″
              ……/>

maxThreads:Tomcat 可创建的最大的线程数,每一个线程处理一个请求;

minSpareThreads: 最小备用线程数,tomcat 启动时的初始化的线程数;

maxSpareThreads: 最大备用线程数,一旦创建的线程超过这个值,Tomcat 就会关闭不再需要的 socket 线程;

acceptCount: 指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,就是被排队的请求数,超过这个数的请求将拒绝连接。

connnectionTimeout: 网络连接超时,单位:毫秒。设置为 0 表示永不超时,这样设置有隐患的。通常可设置为 30000 毫秒。 
enableLookups: 是否允许 DNS 查询

注意:可以多个 connector 公用 1 个线程池。

3、调整连接相关 Connector 的参数:

<Connector executor=”tomcatThreadPool”
              port=”80″ protocol=”HTTP/1.1″ 
              connectionTimeout=”60000″
              keepAliveTimeout=”15000″
              maxKeepAliveRequests=”1″
              redirectPort=”443″
              maxHttpHeaderSize=”8192″ URIEncoding=”UTF-8″ enableLookups=”false” acceptCount=”100″ disableUploadTimeout=”true”/>

参数说明:

  • connectionTimeout – 网络连接超时,单位:毫秒。设置为 0 表示永不超时,这样设置有隐患的。通常可设置为 30000 毫秒。
  • keepAliveTimeout – 长连接最大保持时间(毫秒)。此处为 15 秒。
  • maxKeepAliveRequests – 最大长连接个数(1 表示禁用,- 1 表示不限制个数,默认 100 个。一般设置在 100~200 之间)the maximum number of HTTP requests that can be held in the pipeline until the connection is closed by the server. Setting this attribute to 1 disables HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 allows an unlimited number of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100.
  • maxHttpHeaderSize – http 请求头信息的最大程度,超过此长度的部分不予处理。一般 8K。
  • URIEncoding – 指定 Tomcat 容器的 URL 编码格式。
  • acceptCount – 指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理,默认为 10 个。defines 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 are refused. The default value is 10.
  • disableUploadTimeout – 上传时是否使用超时机制
  • enableLookups – 是否反查域名,取值为:true 或 false。为了提高处理能力,应设置为 false
  • bufferSize – defines the size (in bytes) of the buffer to be provided for input streams created by this connector. By default, buffers of 2048 bytes are provided.
  • maxSpareThreads – 做多空闲连接数,一旦创建的线程超过这个值,Tomcat 就会关闭不再需要的 socket 线程 the maximum number of unused request processing threads that are allowed to exist until the thread pool starts stopping the unnecessary threads. The default value is 50.
  • maxThreads – 最多同时处理的连接数,Tomcat 使用线程来处理接收的每个请求。这个值表示 Tomcat 可创建的最大的线程数。。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.
  • minSpareThreads – 最小空闲线程数,Tomcat 初始化时创建的线程数 the number of request processing threads that are created when this Connector is first started. The connector will also make sure it has the specified number of idle processing threads available. This attribute should be set to a value smaller than that set for maxThreads. The default value is 4.
  • minProcessors – 最小空闲连接线程数,用于提高系统处理性能,默认值为 10。(用于 Tomcat4 中)
  • maxProcessors – 最大连接线程数,即:并发处理的最大请求数,默认值为 75。(用于 Tomcat4 中)

备注:

Tomcat4 中可以通过修改 minProcessors 和 maxProcessors 的值来控制线程数。

在 Tomcat5+ 主要对以下参数调整
maxThreads
 Tomcat 使用线程来处理接收的每个请求。这个值表示 Tomcat 可创建的最大的线程数。
 acceptCount 
 指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理。
 connnectionTimeout 
 网络连接超时,单位:毫秒。设置为 0 表示永不超时,这样设置有隐患的。通常可设置为 30000 毫秒。
 minSpareThreads 
 Tomcat 初始化时创建的线程数。
 maxSpareThreads 
 一旦创建的线程超过这个值,Tomcat 就会关闭不再需要的 socket 线程。

 4、负载均衡、集群的配置

Tomcat6 支持分布式部署,可以实现集群功能,提高响应能力。

5、

利用 JMX 监控 Tomcat 运行情况,需要手工调整启动参数,如下:

打开 cataline.bat,增加一行

set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.port=10090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=”%CATALINA_BASE%\conf\logging.properties”

linux 下修改 cataline.sh:
JAVA_OPTS=”-Dcom.sun.management.jmxremote.port=10090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=%CATALINA_BASE\conf\logging.properties”

注意 JDK\jre\lib\management\management.properties 文件必须存在。

重新启动 tomcat 节点,然后用 jconsole 连接(此处端口 wei10090)

6、Tomcat 增加一个应用

在 server.xml 的 Host 标签中增加行

<Context displayName=”OA” docBase=”/app/web-apps/GACWP” path=”” />

path 代表上下文名称,空表示是根路径

更多 Tomcat 相关教程见以下内容

CentOS 6.6 下安装配置 Tomcat 环境  http://www.linuxidc.com/Linux/2015-08/122234.htm

RedHat Linux 5.5 安装 JDK+Tomcat 并部署 Java 项目  http://www.linuxidc.com/Linux/2015-02/113528.htm 

Tomcat 权威指南(第二版)(中英高清 PDF 版 + 带书签)  http://www.linuxidc.com/Linux/2015-02/113062.htm 

Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm 

Linux 下使用 Xshell 查看 Tomcat 实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm 

CentOS 64-bit 下安装 JDK 和 Tomcat 并设置 Tomcat 开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm 

CentOS 6.5 下安装 Tomcat  http://www.linuxidc.com/Linux/2015-01/111415.htm 

Tomcat 的详细介绍:请点这里
Tomcat 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-03/129568.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计5401字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中