我正在运行 Debian 喘息。每个用户的文件限制增加到 100000 个。
ulimit -a
和ulimit -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 个描述符
答案1
有两种设置可以限制打开文件的数量:每个进程的限制和系统范围的限制。系统范围的限制由 sysctl 设置fs.file-max
,可以在/etc/sysctl.conf
(在启动时读取)或使用sysctl
命令或通过写入/proc/sys/fs/file-max
.每个进程的限制由 设定ulimit -n
。
每个进程的限制由每个进程从其父进程继承。可以在中设置默认值/etc/security/limits.conf
,但这仅适用于交互式会话,不适用于在引导时启动的守护进程。仅当守护程序通过交互式会话启动时,它才适用于守护程序。
要增加(或减少)守护程序的每个进程限制,通常需要编辑其启动脚本并ulimit
在守护程序启动之前添加一个调用。 Debian redis 软件包在单独的文件中提供了配置设置:/etc/default/redis
.注释掉该ULIMIT=
行并根据需要增加该值。