通常我們通過(guò)終端連接到
linux系統(tǒng)后執(zhí)行ulimit -n 命令可以看到本次登錄的session其文件描述符的限制,如下:
$ulimit -n
1024
當(dāng)然可以通過(guò)ulimit -SHn 102400 命令來(lái)修改該限制,但這個(gè)變更只對(duì)當(dāng)前的session有效,當(dāng)斷開(kāi)連接重新連接后更改就失效了。
如果想永久變更需要修改/etc/security/limits.conf 文件,如下:
vi /etc/security/limits.conf
* hard nofile 102400
* soft nofile 102400
保存退出后重新登錄,其最大文件描述符已經(jīng)被永久更改了。
這只是修改用戶級(jí)的最大文件描述符限制,也就是說(shuō)每一個(gè)用戶登錄后執(zhí)行的程序占用文件描述符的總數(shù)不能超過(guò)這個(gè)限制。
系統(tǒng)級(jí)的限制
它是限制所有用戶打開(kāi)文件描述符的總和,可以通過(guò)修改內(nèi)核參數(shù)來(lái)更改該限制:
sysctl -w fs.file-max=102400
使用sysctl命令更改也是臨時(shí)的,如果想永久更改需要在/etc/sysctl.conf添加
fs.file-max=102400
保存退出后使用sysctl -p 命令使其生效。
與file-max參數(shù)相對(duì)應(yīng)的還有file-nr,這個(gè)參數(shù)是只讀的,可以查看當(dāng)前文件描述符的使用情況。
直接修改內(nèi)核參數(shù),無(wú)須重啟系統(tǒng)。
sysctl -w fs.file-max 65536
或者
echo "65536" > /proc/sys/fs/file-max
兩者作用是相同的,前者改內(nèi)核參數(shù),后者直接作用于內(nèi)核參數(shù)在虛擬文件系統(tǒng)(procfs, psuedo file system)上對(duì)應(yīng)的文件而已。
可以用下面的命令查看新的限制
sysctl -a | grep fs.file-max
或者
cat /proc/sys/fs/file-max
修改內(nèi)核參數(shù)
/etc/sysctl.conf
echo "fs.file-max=65536" >> /etc/sysctl.conf
sysctl -p
查看當(dāng)前file handles使用情況:
sysctl -a | grep fs.file-nr
或者
cat /proc/sys/fs/file-nr
825 0 65536
另外一個(gè)命令:
lsof | wc -l
下面是摘自kernel document中關(guān)于file-max和file-nr參數(shù)的說(shuō)明
file-max & file-nr: The kernel allocates file handles dynamically, but as yet it doesn't free them again. 內(nèi)核可以動(dòng)態(tài)的分配文件句柄,但到目前為止是不會(huì)釋放它們的 The value in file-max denotes the maximum number of file handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit. file-max的值是linux內(nèi)核可以分配的最大文件句柄數(shù)。如果你看到了很多關(guān)于打開(kāi)文件數(shù)已經(jīng)達(dá)到了最大值的錯(cuò)誤信息,你可以試著增加該值的限制 Historically, the three values in file-nr denoted 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 handles -- this is not an error, it just means that the number of allocated file handles exactly matches the number of used file handles. 在kernel 2.6之前的版本中,file-nr 中的值由三部分組成,分別為:1.已經(jīng)分配的文件句柄數(shù),2.已經(jīng)分配單沒(méi)有使用的文件句柄數(shù),3.最大文件句柄數(shù)。但在kernel 2.6版本中第二項(xiàng)的值總為0,這并不是一個(gè)錯(cuò)誤,它實(shí)際上意味著已經(jīng)分配的文件句柄無(wú)一浪費(fèi)的都已經(jīng)被使用了 |
English » | | | | | | | | |
Text-to-speech function is limited to 100 characters