不可行的方法
最初我直接修改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進程。
我的方式,參考上面參數設置啟發,設置了-Xms1G -Xmn1G就好了,shutdown.sh就能用了