Linux对可以或许打开的文件句柄的数目做了限定。限定是分为三个层面:
体系层面、用户层面、历程层面。
假如某个历程打开的文件句柄高出限定,再打开文件就会报错。
Too many open files 大概 Socket/File: Can’t open so many files因此,在编程时,假如历程要占用大量文件句柄的话,就要看看是否要调整最大可打开的文件句柄数,同时也要注意close开释掉不消的文件句柄。
查看文件句柄数限定
ps -ef|grep javaroot 3146 1 3 09:54 ? 00:10:08 /usr/local/jdk1.8.0_11/bin/java -server -jar /usr/local/tomcat/qhdzs/qhd_private.jar --spring.profiles.active=testcat /proc/3146/limitsLimit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 15591 15591 processes Max open files 4096 4096 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 15591 15591 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us Max open files 4096 4096 files这就是历程级别的打开文件句柄数目限定体系级限定
理论上体系内存有多少就可以打开多少的文件句柄,但是在现实中内核是会做相应的处置惩罚,一般最大打开文件数会是体系内存的10%(以KB来盘算),称之为体系级限定。这个数字可以通过 cat /proc/sys/fs/file-max 大概 sysctl -a | grep fs.file-max 下令查看。
在Linux下,体系全部可以或许打开的fd(文件句柄)总数为:/proc/sys/fs/file-max,取决于内存
# The file-max parameter# file-max参数The file-max file /proc/sys/fs/file-max sets the maximum number of file-handles that the Linux kernel will allocate. We generally tune this file to improve the number of open files by increasing the value of /proc/sys/fs/file-max to something reasonable like 256 for every 4M of RAM we have: i.e. for a machine with 128 MB of RAM, set it to 8192 - 128/4=32 32*256=8192.# file-max file /proc/sys/fs/file-max设置Linux内核将分配的最大文件句柄数目。我们通常通过将/proc/sys/fs/file-max的值增长到一个公道的值来调整这个文件,比如每4M内存增长256个文件;也就是说,对于一台有128MB内存的呆板,将其设置为8192 128M/4M=32个 32*256=8192.The default setup for the file-max parameter under Red Hat Linux is: "4096" To adjust the value of file-max to 128 MB of RAM, type the following on your terminal:# Red Hat Linux下file-max参数的默认设置是“4096”要将file-max的值调整为128MB 内存,必要在终端实验以下下令[root@deep] /# echo "8192" >/proc/sys/fs/file-maxAdd the above commands to the /etc/rc.d/rc.local script file and you'll not have to type it again the next time your server reboots.# 将上述下令添加到/etc/rc.d/rc.local脚本文件中,下次服务器重新启动时,就不必再实验它了。Edit the /etc/sysctl.conf file and add the following line:编辑 /etc/sysctl.conf 文件并添加以下行: # Improve the number of open files fs.file-max = 8192You must restart your network for the change to take effect. The command to manually restart the network is the following:# 必须重启网络,以使得更改收效。重启网络的下令如下: [root@deep] /# /etc/rc.d/init.d/network restart Setting network parameters [ OK ] Bringing up interface lo [ OK ] Bringing up interface eth0 [ OK ] Bringing up interface eth1 [ OK ] When you regularly receive from your server a lot of messages with errors about running out of open files, you might want to raise this limit. The default value is 4096. A file server or web server needs a lot of open files# 当你经常从服务器收到大量关于打开文件不敷的错误消息时,就必要进步此限定,默认值为4096,文件服务器大概web服务器必要大量的打开文件。文献资料来自:http://www.faqs.org/docs/securing/chap6sec72.html
[root@anolis ~]# free -m total used free shared buff/cache availableMem: 3720 227 3235 8 257 3267Swap: 4043 0 40433720/4=930 930*256=238080这台服务器上的 fs.file-max参数的理论最大值可以设置为238080。用户级别限定/etc/security/limits.conf