简单的说, max-file表示系统级别的能够打开的文件句柄的数量,
file-max中指定了系统范围内所有进程可打开的文件句柄的数量限制(系统级别, kernel-level). (The value in file-max denotes the maximum number of file handles that the Linux kernel will allocate).当收到”Too many open files in system”这样的错误消息时, 就应该曾加这个值了.
/proc/sys/fs/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-pro-
cess 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:
echo 100000 > /proc/sys/fs/file-max
The kernel constant NR_OPEN imposes an upper limit on the value that may be placed in file-max.
If you increase /proc/sys/fs/file-max, be sure to increase /proc/sys/fs/inode-max to 3-4 times the new value of /proc/sys/fs/file-max, or you will run out of
inodes.
/proc/sys/fs/file-nr
Historically, the kernel was able to allocate file handles dynamically, but not to free them again. The three values in file-nr denote the number of allocated
file handles, the number of allocated but unused file handles, and the maximum number of file handles. Linux 2.6 always reports 0 as the number of free file han-
dles -- this is not an error, it just means that the number of allocated file handles exactly matches the number of used file handles.
# cat /proc/sys/fs/file-max
185230
# echo 100000 > /proc/sys/fs/file-max
或者
# echo ""fs.file-max=65535" >> /etc/sysctl.conf
# sysctl -p
对于2.2的内核, 还需要考虑inode-max, 一般inode-max设置为file-max的4倍. 对于2.4及以后的内核, 没有inode-max这个文件了.
file-nr 可以查看系统中当前打开的文件句柄的数量. 他里面包括3个数字: 第一个表示已经分配了的文件描述符数量, 第二个表示空闲的文件句柄数量, 第三个表示能够打开文件句柄的最大值(跟file-max一致). 内核会动态的分配文件句柄, 但是不会再次释放他们(这个可能不适应最新的内核了, 在我的file-nr中看到第二列一直为0, 第一列有增有减)
man bash, 找到说明ulimit的那一节:
提供对shell及其启动的进程的可用资源(包括文件句柄, 进程数量, core文件大小等)的控制. 这是进程级别的, 也就是说系统中某个session及其启动的每个进程能打开多少个文件描述符, 能fork出多少个子进程等…
当达到上限时, 会报错”Too many open files”或者遇上Socket/File: Can’t open so many files等
另外需要注意的是, 每种资源都有相关的软硬限制, 软限制是内核强加给相应资源的限制值,硬限制是软限制的最大值. 非授权调用进程只可以将其软限制指定为0~硬限制范围中的某个值,同时能不可逆转地降低其硬限制.授权进程可以任意改变其软硬限 制.RLIM_INFINITY的值表示不对资源限制.
分别使用-H和-S选项来指定需要对资源是做硬限制/软限制的设置. 如果都不指定, 硬限制和软限制同时设置.
打印资源的限制值, 如果不明确指定-H, 打印的是-