tomcat运行一段时间后报错"Too many open files"

tomcat运行一段时间后报打开太多文件错误:Too many open files

 

 查看当前进程的文件打开数:

lsof -n |awk '{print $2}'|sort|uniq -c |sort -nr|more
或
ps -ef | grep 进程号或进程名称| grep -v "grep" | awk '{print $2}'

PS:第一行是打开的文件句柄数量,第二行是进程号。得到进程号后,我们可以通过ps命令得到进程的详细内容

原因:

  可能是系统内存资源消耗问题,也有可能是程序问题。当然也有可能是系统运行时进程打开的文件数超过了系统自定义的值。

修改文件打开数(最大打开句柄数):

1)临时修改,退出shell时无效

 

ulimit -n 65535

 

2)修改profile文件,在文件最后一行添加。只对当前用户 有效

[root@VM_0_10_centos ~]# vi /etc/profile
ulimit -n 65535
[root@VM_0_10_centos ~]# source /etc/profile
[root@VM_0_10_centos ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7269
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7269
virtual memory          (kbytes, -v) unlimited
file locks

3)修改/etc/security/limits.conf文件,在文件最后添加下面两行内容。立即生效

* soft nofile 65536 #限制单个进程最大文件句柄数(到达此限制时系统报警) 
* hard nofile 65536 #限制单个进程最大文件句柄数(到达此限制时系统报错)

4)修改系统的最大打开文件句柄数

vi /etc/sysctl.conf
fs.file-max=6553560    #限制整个系统最大文件句柄数 
/sbin/sysctl -p               #使配置生效    

解决:

  我这边是重启服务之后就正常了,后面设置了文件打开数,过几天看会不会还有这个情况出现,如果还有,可能是程序代码问题

 

posted @ 2019-10-23 10:12  HeiDi_BoKe  阅读(556)  评论(0编辑  收藏  举报