您现在的位置是:首页 >  云笔记 >  开发随笔 >  文章详情

linux上Tomcat 7执行shutdown.sh报错Cannot allocate memory

特不靠谱   2020-09-18 21:45:49   310人已围观

linux上Tomcat 7执行shutdown.sh报错Cannot allocate memory报错信息:

[ bin]# ./shutdown.sh 
Using CATALINA_BASE:   /xxxxxx/tomcat
Using CATALINA_HOME:   /xxxxxx/tomcat
Using CATALINA_TMPDIR: /xxxxxx/tomcat/temp
Using JRE_HOME:        /xxxxxx/jdk/jre
Using CLASSPATH:       /xxxxxx/tomcat/bin/bootstrap.jar:/xxxxxx/tomcat/bin/tomcat-juli.jar
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000ce000000, 570425344, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 570425344 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /xxxxxx/tomcat/bin/hs_err_pid14506.log


找到hs_err_pid14506.log

[root@root bin]# vi hs_err_pid14506.log 
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 570425344 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:2756), pid=14534, tid=140270371923712
#
# JRE version:  (7.0_79-b15) (build )
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.79-b02 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
#
Memory: 4k page, physical 1883492k(73904k free), swap 0k(0k free)
vm_info: Java HotSpot(TM) 64-Bit Server VM (24.79-b02) for linux-amd64 JRE (1.7.0_79-b15), built on Apr 10 2015 11:34:48 by "java_re" with gcc 4.3.0 20080428 (Red Hat 4.3.0-8)
time: Wed Jul 22 22:27:43 2020
elapsed time: 0 seconds

根本原因:

     服务器上物理内存太小,大部分都是应为程序太多,内存吃紧,而给jvm分配的内存太大,java程序启动需要的内存,linux不能给,所以就冲突了;

解决方案:

     解决方案基本上就2个方向,有钱有必要你就升级服务器内存,加内存,没钱或者没必要那就减少内存使用降低JVM内存分配,第一种没啥好说的,这里我主要想记录一下如何调整jvm内存配置:

     在这里我以我自己的一台测试机器为例,机器的配置如下:

     CPU:1核,内存:2GB,操作系统:CentOS 64位 ,上面安装了一个mysql数据库(占用内存35%)和一个tomcat(占用内存40%),其他的主要是操作系统和上面的监控程序。其中的tomcat的内存配置如下:

# OS specific support.  $var _must_ be set to either true or false.
JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.awt.headless=true"
cygwin=false

基于以上的情况做的优化:

1.调整tomca中对jvm内存的配置:

  关于tomcat的JVM参数说明可以参考我前面的一遍文章:https://www.onekbit.com/ViewBlog/blog/BID20200705100444

  所以解决方案:JAVA_OPTS="-server -Xms600m -Xmx600m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.awt.headless=true" 通常会将-Xms 与-Xmx两个参数的配置相同的值,其目的是为了能够在java垃圾回收机制清理完堆区后不需要重新分隔计算堆区的大小而浪费资源。

2、修改配置文件,catalina.sh文件:

cd 到tomcat的/bin目录下,找到catalina.sh文件

vi 命令 vi  catalina.sh 进入编辑状态,找到   

# OS specific support. $var _must_ be set to either true or false.  

JAVA_OPTS="-server -Xms600m -Xmx600m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.awt.headless=true"      

cygwin=false

注意,双引号,红色就是放置的位置。没有的需要添加,有的话需要自己修改最小堆内存大小

3、配置完成后重启tomcat即可


分享到:

编辑发布时间:2020-09-18 21:45:49