http://tommwq.tech/blog/2020/11/04/182
file-max的含义
通过man proc,可得到file-max的描述:
This file defines a system-wide limit on the number of open files for all processes. (See also setrlimit(2), which can be used by a process to set the per-process limit, RLIMIT_NOFILE, on the number of files it may open.) If you get lots of error messages about running out of file handles, try increasing this value:
从这里可以知道,file-max是设置系统所有进程总共可以打开的文件数量。如果得到大量使用完文件描述符的错误信息,是应该增加这个值。
如果要临时性修改file-max的值,可以执行下列命令
echo 6553560 > /proc/sys/fs/file-max
如果要永久性修改file-max,打开文件/etc/sysctl.conf, 加入
fs.file-max = 6553560
重启系统后生效。
除了全局的file-max限制,程序可以通过setrlimit函数,设置单个进程的文件描述符数量限制。
ulimit
同样的,先看man
Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.
ulimit是当前shell以及由它启动的进程的资源限制。系统默认的ulimit对文件打开数量的限制是1024,修改/etc/security/limits.conf文件可以调整这一配置。
soft nofile 65535
hard nofile 65535
修改完之后,重启即可生效。
对于服务器,file-max和ulimit都需要进行配置,否则容易出现文件描述符用尽的情况。