posts - 72, comments - 66, trackbacks - 0, articles - 0

          關(guān)于 Exception : Too many open files

          Posted on 2008-10-22 19:13 Fingki.li 閱讀(5841) 評(píng)論(7)  編輯  收藏 所屬分類: About linux
          當(dāng)進(jìn)行大并發(fā)的壓力測(cè)試時(shí),經(jīng)常會(huì)出現(xiàn)如下Exception:Too many open files.
          查閱資料,google baidu.
          首先感謝demo的評(píng)論,使我對(duì)這個(gè)問(wèn)題有了新的認(rèn)識(shí)。
          經(jīng)過(guò)再次查找,發(fā)現(xiàn)這個(gè)問(wèn)題的出現(xiàn)原因是system對(duì)打開files數(shù)量的限制問(wèn)題。
          用 ulimit -a 命令可以查看當(dāng)前所有資源限制
          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:~$
          可以看出,對(duì)open files的限制數(shù)是1024,我們可以通過(guò)修改這個(gè)值來(lái)增加可以打開的文件數(shù)。
          最簡(jiǎn)單的修改方式就是用ulimit -n 命令,
          比如我打算將其改為2048,用 ulimit -n 2048.
          當(dāng)你把open files的值增大到一定程度,你的Too many open files就不會(huì)再出現(xiàn)了。

          而對(duì)于tcp_fin_timeout,是合tcp連接相關(guān)的,當(dāng)你有大量tcp連接時(shí),或許有些性能改善;
          tcp_fin_timeout,默認(rèn)情況下,win為4 min,linux為60 sec.
          可以把其相應(yīng)設(shè)置短一些,以增加系統(tǒng)性能。

          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
          2. Restart the networking component, for example by running
            /etc/init.d/networking restart  or  service network restart
          在linux下,經(jīng)常會(huì)有權(quán)限問(wèn)題使設(shè)置不能成功,盡管你用了sudo。
          解決辦法就是先 su root,在root用戶下來(lái)執(zhí)行操作,這樣就ok了。
          再有就是可能忘記root密碼了,那就 sudo passwd root,來(lái)設(shè)置一個(gè)新密碼。

          根據(jù)進(jìn)一步的研究發(fā)現(xiàn):服務(wù)器默認(rèn)情況下對(duì)進(jìn)程的處理也是有限制的,要想server處理更多用戶進(jìn)程就需要調(diào)整相應(yīng)參數(shù)。
          這里面有兩個(gè)文件要特別注意,
          一個(gè)是    /etc/security/limits.conf
          另一個(gè)    /etc/sysctl.conf

          當(dāng)我們用ulimit -a命令可以查看 open files(默認(rèn)為1024)和max user processes(默認(rèn)也為1024),
          所以默認(rèn)情況下這個(gè)server只允許同時(shí)打開1024個(gè)文件,處理1024個(gè)用戶進(jìn)程,
          若要 臨時(shí) 改變這兩個(gè)參數(shù)值,可以使用 ulimit -n 10240 ,ulimit -u 10240,
          若要 長(zhǎng)久 改變這兩個(gè)參數(shù)值,就要修改/ect/security/limits.conf,在文件中加上兩行:
          * - nofile 102400
          * - nproc 102400

          而對(duì)于大量使用tcp連接的應(yīng)用來(lái)說(shuō),也需要對(duì)/etc/sysctl.conf中的參數(shù)進(jìn)行相應(yīng)優(yōu)化:
          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
          上面是我根據(jù)我的需求的一些參數(shù)調(diào)整,你可以根據(jù)你的需求來(lái)調(diào)整相應(yīng)參數(shù)值。
          然后執(zhí)行 sysctl -p命令可立即生效。sysctl -a可查看參數(shù)值。

          參考 :http://www.javaeye.com/topic/65175

          Feedback

          # re: 關(guān)于 Exception : Too many open files  回復(fù)  更多評(píng)論   

          2008-10-23 08:57 by demo
          你的理解是片面的。事實(shí)也不是你理解的情況。

          # re: 關(guān)于 Exception : Too many open files  回復(fù)  更多評(píng)論   

          2008-10-23 08:57 by demo
          ulimit

          # re: 關(guān)于 Exception : Too many open files  回復(fù)  更多評(píng)論   

          2008-10-23 09:09 by Fingki.li
          @demo
          感謝你的回復(fù),能不能對(duì)Too many open files 給我一些建議?

          # re: 關(guān)于 Exception : Too many open files  回復(fù)  更多評(píng)論   

          2008-10-23 13:02 by falk
          你這不是根本解決方案。 ulimit

          # re: 關(guān)于 Exception : Too many open files  回復(fù)  更多評(píng)論   

          2008-10-23 14:46 by Fingki.li
          @falk
          哦?但我這樣做的確不再報(bào) Too many open files 了。
          你能給我一些更好的建議嗎?

          # re: 關(guān)于 Exception : Too many open files[未登錄](méi)  回復(fù)  更多評(píng)論   

          2011-08-26 10:22 by 林海
          這樣確實(shí)沒(méi)有解決問(wèn)題的根本,還是自己的程序有問(wèn)題,沒(méi)有把句柄安全關(guān)閉。
          如果關(guān)閉了就不會(huì)出現(xiàn)這種提示,那怕系統(tǒng)句柄上限只能打開10個(gè)文件句柄,也不會(huì)報(bào)錯(cuò)。

          # re: 關(guān)于 Exception : Too many open files[未登錄](méi)  回復(fù)  更多評(píng)論   

          2011-08-26 10:26 by 林海
          做為服務(wù)器的話,如果同時(shí)間有N個(gè)連接,可能會(huì)發(fā)生,但很少有服務(wù)器會(huì)同時(shí)打開1024個(gè)文件句柄吧? 總有先會(huì),注意釋放資源。
          主站蜘蛛池模板: 息烽县| 红河县| 隆德县| 青神县| 新绛县| 三穗县| 绥棱县| 巧家县| 鄂伦春自治旗| 巴彦淖尔市| 新源县| 侯马市| 无棣县| 泌阳县| 乌恰县| 纳雍县| 中山市| 鄄城县| 杭锦后旗| 阳高县| 香港 | 安平县| 文昌市| 灵山县| 尤溪县| 噶尔县| 大渡口区| 南京市| 双柏县| 右玉县| 贵州省| 衡水市| 茂名市| 搜索| 宁安市| 汝南县| 天门市| 越西县| 绥德县| 张掖市| 阿巴嘎旗|