如何调节tomcat初始内存

时间:2023-03-10 06:54:09
如何调节tomcat初始内存

1.linux下调节tomcat初始内存大小

linux下tomcat的运行文件为catalina.sh,打开文件,在文件靠近顶部找到“ JAVA_OPTS ”字样,在它后面添加如下内容即可

#   JAVA_OPTS       (Optional) Java runtime options used when any command
# is executed.
# Include here and not in
CATALINA_OPTS all options, that
# should be used by Tomcat and also by the stop process,
# the version command etc.
# Most options should go into CATALINA_OPTS.
#

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms700m -Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=1024m -XX:PermSize=512m -XX:MaxPermSize=512m"
# JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories
# containing some jars in order to allow replacement of APIs
# created outside of the JCP (i.e. DOM and SAX from W3C).
# It can also be used to update the XML parser implementation.
# Defaults to $CATALINA_HOME/endorsed.

2.windows下调节tomcat初始内存大小

windows下tomcat的运行文件为catalina.bat,打开文件,在文件中部找到“rem ----- Execute The Requested Command -----”字样 ,在它后面添加如下内容即可

set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER%"

rem ----- Execute The Requested Command ---------------------------------------

set JAVA_OPTS=%JAVA_OPTS% -server -Xms2048m -Xmx2048m -Xmn512m -XX:PermSize=400m -XX:MaxPermSize=
700m

echo Using CATALINA_BASE:
"%CATALINA_BASE%"
echo Using CATALINA_HOME2: "%CATALINA_HOME2%"
echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
if ""%"" == ""debug"" goto use_jdk
echo Using JRE_HOME: "%JRE_HOME%"
goto java_dir_displayed
:use_jdk
echo Using JAVA_HOME: "%JAVA_HOME%"
:java_dir_displayed
echo Using CLASSPATH: "%CLASSPATH%"

需要注意的是上面各值的大小需要根据服务器本身的内存大小来设置

3.参数说明

堆区:
-Xms
初始堆大小,默认是物理内存的1/64
-Xmx 最大堆大小,物理内存的1/4
空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制;空余堆内存大于70%时,JVM会减少堆直到 -Xms的最小限制
因此服务器一般设置-Xms、-Xmx 相等,生产环境建议设为1024m以上
-Xmn 年轻代大小 -XX:NewSize 设置年轻代大小
-XX:MaxNewSize 年轻代最大值 非堆区:
-XX:PermSize 设置持久代(perm gen)初始值(非堆内存初始值),物理内存的1/64
-XX:MaxPermSize 设置持久代最大值(最大非堆内存),物理内存的1/4