當進行大并發的壓力測試時,經常會出現如下Exception:Too 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,默認情況下,win為4 min,linux為60 sec.
可以把其相應設置短一些,以增加系統性能。
in Windows
- Run regedit to start the Registry Editor
- Locate the following key: HKEY_LOCAL_MACHINE"System"CurrentControlSet"Services"tcpip"Parameters
- Add a new value named TcpTimedWaitDelay asa decimal and set the desired timeout in seconds (30-300)
- Reboot
in Linux
- Update the configuration value by running (30 seconds used in the example)
echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
- 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可查看參數值。
參考 :http://www.javaeye.com/topic/65175