Debian

即使系統限制已增加,也已達到守護程序的打開文件限制

  • October 12, 2014

我正在執行 Debian wheezy。每個使用者的文件限制增加到 100000 個。 ulimit -aulimit -Hn/-Sn即使在螢幕上也顯示正確數量的最大打開文件限制。但由於某種原因,我無法擁有超過 ~4000 個連接/打開的文件。

來自sysctl.conf

net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 500 65000
net.core.somaxconn = 81920

輸出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) 256639
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 999999
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) 256639
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

例如redis:

客戶:100 個客戶的基準測試

Writing to socket: Connection reset by peer
Writing to socket: Connection reset by peer
Writing to socket: Connection reset by peer
Writing to socket: Connection reset by peer
Error: Connection reset by peer

伺服器資訊:

127.0.0.1:6379> info clients
-Clients
connected_clients:4005
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

爪哇:

Caused by: io.netty.channel.ChannelException: Failed to open a socket.
Caused by: java.net.SocketException: Too many open files
       at sun.nio.ch.SelectorProviderImpl.openSocketChannel(Unknown Source)
java.io.IOException: Too many open files
Caused by: io.netty.channel.ChannelException: Failed to open a socket.
Caused by: java.net.SocketException: Too many open files
       at sun.nio.ch.SelectorProviderImpl.openSocketChannel(Unknown Source)
Caused by: io.netty.channel.ChannelException: Failed to open a socket.
Caused by: java.net.SocketException: Too many open files

ls -l /proc/[id]/fd | wc -l顯示約 4000 個描述符

有兩個設置可以限制打開文件的數量:每個程序的限制和系統範圍的限制。系統範圍的限制由fs.file-maxsysctl 設置,可以在/etc/sysctl.conf(引導時讀取)中配置或使用sysctl命令或通過寫入動態設置/proc/sys/fs/file-max。每個程序的限制由 設置ulimit -n

每個程序的限制由每個程序從其父程序繼承。可以在 中設置預設值/etc/security/limits.conf,但這僅適用於互動式會話,不適用於在引導時啟動的守護程序。只有當它通過互動式會話啟動時,它才會應用於守護程序。

ulimit要增加(或減少)守護程序的每個程序限制,通常,編輯它的啟動腳本並在守護程序啟動之前添加一個呼叫。Debian redis 軟體包在一個單獨的文件中附帶了一個配置設置:/etc/default/redis. 註釋掉該ULIMIT=行並在必要時增加值。

引用自:https://unix.stackexchange.com/questions/161724