1. 程式人生 > >tomcat記憶體溢位問題解決思路

tomcat記憶體溢位問題解決思路

1、修改啟動時記憶體引數、並指定JVM時區 (在windows server 2008 下時間少了8個小時)

在Tomcat上執行j2ee專案程式碼時,經常會出現記憶體溢位的情況,解決辦法是在系統引數中增加系統引數: 

window下, 在catalina.bat最前面:
set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m
一定加在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" connectionTimeout="60000" keepAliveTimeout="15000" maxKeepAliveRequests="1" redirectPort="443" ....../> 注意:可以多個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代表上下文名稱,空表示是根路徑。 轉載地址:http://blog.sina.com.cn/cchao06