不可行的方法
最初我直接修改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就能用了