解决tomcat无法启动的问题

时间:2024-03-31 09:20:59

今天项目的tomcat突然挂掉了,之前的解决方法一直都是直接重启tomcat即可:

service tomcat restart

然而今天重启tomcat后查看状态发现启动状态一直为“failed”

解决tomcat无法启动的问题

一开始以为是pid和进程号对不上的问题,于是检查了一下/tmp/tomcat.pid,发现是能对上的。

查看tomcat的日志catalina.out,发现报错:

# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2796814336 bytes for committing reserved memory.
# An error report file with more information is saved as:
# //hs_err_pid26374.log

于是再去看hs_err_pid26374.log文件:

#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2796814336 bytes for committing reserved memory.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full
#   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:2749), pid=31196, tid=0x00007feadb62a700
#
# JRE version:  (8.0_201-b09) (build )
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode linux-amd64 compressed oops)
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again

发现好像是内存不足的原因,于是百度了一下,查找了一些解决方案:

http://zoutairan.com/

https://www.yangbajing.me/2019/05/30/%E8%BD%AC%E5%8F%91%EF%BC%9A%E4%B8%80%E6%AC%A1%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E6%8A%A5%E9%94%99outofmemory-error%E7%9A%84%E5%A4%84%E7%90%86%E8%AE%B0%E5%BD%95/

首先看了下服务器的空闲内存 free -m:

解决tomcat无法启动的问题

内存貌似比较小,于是猜测大概率是服务器内存不足的原因,于是开始尝试修改jvm的内存,一开始看了很多资料都是在catalina.sh文件里的JVM_OPTS里修改内存,于是我在该文件里加上了:

JAVA_OPTS="$JAVA_OPTS -server -Xss228k -Xms256m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=1024m"

重启tomcat之后发现并没有用。另外我发现用tomcat service start 和直接用/tomcat/bin/startup.sh命令貌似效果不太一样,于是我想到是不是在服务化的时候设置了jvm的参数,于是查看了一下服务化的文件:tomcat.service,发现里面果然有java虚拟机的设置:

Environment='CATALINA_OPTS=-server -Xmx4800m -Xms3000m -XX:MaxPermSize=4096m -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ParallelGCThreads=4 -Djavax.servlet.request.encoding=UTF-8 -Djavax.servlet.response.encoding=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=GMT+08 -Djava.security.egd=file:/dev/./urandom'

把这些参数里的内存调小之后,tomcat可以正常启动了。