Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3232294
  • 博文数量: 346
  • 博客积分: 10189
  • 博客等级: 上将
  • 技术积分: 3125
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-05 19:46
文章分类

全部博文(346)

文章存档

2013年(35)

2011年(35)

2010年(76)

2009年(48)

2008年(152)

分类: LINUX

2010-05-18 13:10:57

关于 Exception : Too many open files

http://www.blogjava.net/fingki/archive/2008/10/22/236000.html

当进行大并发的压力测试时,经常会出现如下ExceptionToo many open files.
查阅资料,google baidu.
首先感谢demo的评论,使我对这个问题有了新的认识。
经过再次查找,发现这个问题的出现原因是system对打开files数量的限制问题。
ulimit -a 命令可以查看当前所有资源限制
fingki@ubuntu:~$ 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) 15863
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
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15863
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
fingki@ubuntu:~$ 
可以看出,对open files的限制数是1024,我们可以通过修改这个值来增加可以打开的文件数。
最简单的修改方式就是用ulimit -n 命令,
比如我打算将其改为2048,用 ulimit -n 2048.
当你把open files的值增大到一定程度,你的Too many open files就不会再出现了。

而对于tcp_fin_timeout,是合tcp连接相关的,当你有大量tcp连接时,或许有些性能改善;
tcp_fin_timeout,默认情况下,win4 min,linux60 sec.
可以把其相应设置短一些,以增加系统性能。

in Windows

  1. Run regedit to start the Registry Editor
  2. Locate the following key: HKEY_LOCAL_MACHINE"System"CurrentControlSet"Services"tcpip"Parameters
  3. Add a new value named TcpTimedWaitDelay asa decimal and set the desired timeout in seconds (30-300)
  4. Reboot

in Linux

  1. Update the configuration value by running (30 seconds used in the example)

echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout

  1. Restart the networking component, for example by running

/etc/init.d/networking restart  or  service network restart

linux下,经常会有权限问题使设置不能成功,尽管你用了sudo
解决办法就是先 su root,在root用户下来执行操作,这样就ok了。
再有就是可能忘记root密码了,那就 sudo passwd root,来设置一个新密码。

根据进一步的研究发现:服务器默认情况下对进程的处理也是有限制的,要想server处理更多用户进程就需要调整相应参数。
这里面有两个文件要特别注意,
一个是    /etc/security/limits.conf 
另一个    /etc/sysctl.conf 

当我们用ulimit -a命令可以查看 open files(默认为1024)max user processes(默认也为1024),
所以默认情况下这个server只允许同时打开1024个文件,处理1024个用户进程,
若要 临时 改变这两个参数值,可以使用 ulimit -n 10240 ,ulimit -u 10240,
若要 长久 改变这两个参数值,就要修改/ect/security/limits.conf,在文件中加上两行:
* - nofile 102400
* - nproc 102400

而对于大量使用tcp连接的应用来说,也需要对/etc/sysctl.conf中的参数进行相应优化:
net/ipv4/ip_always_defrag = 1
net.ipv4.ip_local_port_range = 1024    65000
net.ipv4.tcp_max_syn_backlog = 102400
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
上面是我根据我的需求的一些参数调整,你可以根据你的需求来调整相应参数值。
然后执行 sysctl -p命令可立即生效。sysctl -a可查看参数值。

阅读(2477) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~