遭遇”java.io.IOException: Too many open files”

今天下午,线上的Jetty服务宕机了,日志里大量的"java.io.IOException: Too many open files"错误。搜索到Freddy Chu的博客里正好有关于此的解决办法。

$ sysctl fs.file-max
$ lsof -u jetty -nn | wc -l
$ ulimit -a
$ sudo vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

同时发现了系统里也有Freddy Chu提到的"Too many CLOSE_WAIT"问题,谢谢Freddy Chu。

$ sudo vi /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_intvl = 2
net.ipv4.tcp_keepalive_probes = 2
net.ipv4.tcp_keepalive_time = 1800

$ sudo sysctl -p

Update(2010-01-21): root用户要特殊设置

$ sudo vi /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535