文章

java.io.FileNotFoundException: (打开的文件过多)

tomcat服务器运行程序访问报错 java.io.FileNotFoundException 打开的文件过多。
在使用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) 64048
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 32768
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

然后使用cat /proc/$(pgrep java)/limits | grep "Max open files"查看java进程的最大文件打开数配置发现是4096

Max open files            4096                4096                files

因为tomcat是用rc-local来实现开机启动的,而rc-local默认没有对文件打开数进行设置,编辑/usr/lib/systemd/system/rc-local.service添加LimitNOFILE=65535参数,限制数量自行设置

[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
LimitNOFILE=65535

修改后运行

systemctl daemon-reload
systemctl restart rc-local

重启完毕后查看进程最大文件打开数cat /proc/$(pgrep java)/limits | grep "Max open files"

Max open files            65535                65535                files
License:  CC BY 4.0