不可行的方法
最初我直接改动catalina.sh
, 将JAVA_OPTS
变量加上了
-server -Xms1G -Xmx1G -XX:+UserG1GC
最初看起来没啥问题,可是当服务器运行几天后,发现运行shutdown.sh
无法关闭tomcat, 错误信息例如以下:
# root@iZ94hjppdqzZ:~/projects/taolijie# cat hs_err_pid5519.log
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1073741824 bytes for committing reserved memory.
# Possible reasons:
# The system is out of physical RAM or swap space
# In 32 bit mode, the process size limit was hit
# Possible solutions:
# Reduce memory load on the system
# Increase physical memory or swap space
# Check if swap backing store is full
# Use 64 bit Java on a 64 bit OS
# Decrease Java heap size (-Xmx/-Xms)
# Decrease number of Java threads
# Decrease Java thread stack sizes (-Xss)
# Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.
#
# Out of Memory Error (os_linux.cpp:2673), pid=5519, tid=3061726064
#
# JRE version: (8.0_45-b14) (build )
# Java VM: Java HotSpot(TM) Server VM (25.45-b02 mixed mode linux-x86 )
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
关闭个tomcat要请求1G的内存, 当时服务器就剩下200M内存,所以Out Of Memory了。查看shutdown.sh
,发现它事实上还是调用了catalina.sh
,仅仅是传入了一个stop
參数。而catalina.sh
是运行了
org.apache.catalina.startup.Bootstrap stop
来向tomcat发送关闭信息的。因为上面我们设置了JAVA_OPTS
使用1G的堆。因此运行该类时JVM会向系统申请1G多的内存,直接导致Out Of Memory。
可行的方法
在catalina.sh
的第二行加入:
CATALINA_OPTS="$CATALINA_OPTS -server -Xms1G -Xmx1G -XX:+UseG1GC"
这些VM參数就会仅仅应用到catalina而不是全部Tomcat进程。