解决linux下too many file问题
网址同时访问量比较大,此时jetty就会报错,报错信息中包含“Too many open files”,例如:
java.io.FileNotFoundException: (Too many open files)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at weblogic.descriptor.DescriptorCache.writeToCache(DescriptorCache.java:236)
at weblogic.descriptor.DescriptorCache.parseXML(DescriptorCache.java:388)
Truncated. see log file for complete stacktrace
linux默认的打开文件数量是1024,我们可以用ulimit -a 来查看系统资源,例如:
[root@redhat ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024 --打开最大文件数量限制
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 16384
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
解决该问题的宗旨是:增大打开文件最大句柄限制数,该数是1024的整数倍
1.临时增大open files的限制值
可以用ulimit -n 来临时增大该限制值,但是一旦重启服务器后,该值又会恢复到1024。只能暂时性的解决问题。命令如下:
sudo sh -c "ulimit -n 65535 && exec su $LOGNAME"
However, while you do need to be root to raise the limit to 65535, you probably don’t want to run your program as root. So after you raise the limit you should switch back to the current user.
To do this, run:
sudo sh -c "ulimit -n 65535 && exec su $LOGNAME"
and you will get a new shell, without root privileges, but with the raised limit. The exec causes the new shell to replace the process with sudo privileges, so after you exit that shell, you won’t accidentally end up as root again
下面这个命令可以追踪程序打开了什么文件
lsof -p <pid of jvm>
永久修改打开文件的最大数可以修改 /etc/security/limits.conf
加入下面2句
* soft nofile 2048 # Set the limit according to your needs * hard nofile 2048
然后重启putty, 可以看到生效了