linux 修改最大文件描述符

linux下最大文件描述符的限制有兩個方面,一個是用戶級的限制,另一個則是系統級限制。linux

先介紹如何修改系統級的限制

一般咱們經過終端鏈接到linux系統後執行ulimit -n 命令能夠看到本次登陸的session其文件描述符的限制,以下:
$ulimit -n
1024session

固然能夠經過ulimit -SHn 102400 命令來修改該限制,但這個變動只對當前的session有效,當斷開鏈接從新鏈接後更改就失效了。ide

若是想永久變動須要修改/etc/security/limits.conf 文件,以下:
vi /etc/security/limits.conf
* hard nofile 102400
* soft nofile 102400this

保存退出後從新登陸,其最大文件描述符已經被永久更改了。spa

這只是修改用戶級的最大文件描述符限制,也就是說每個用戶登陸後執行的程序佔用文件描述符的總數不能超過這個限制。.net

系統級的限制

它是限制全部用戶打開文件描述符的總和,能夠經過修改內核參數來更改該限制:
sysctl -w fs.file-max=102400code

使用sysctl命令更改也是臨時的,若是想永久更改須要在/etc/sysctl.conf添加
fs.file-max=102400
保存退出後使用sysctl -p 命令使其生效。orm

與file-max參數相對應的還有file-nr,這個參數是隻讀的,能夠查看當前文件描述符的使用狀況。three

sysctl -a | grep file-nr內存

下面是摘自kernel document中關於file-max和file-nr參數的說明

file-max & file-nr:

The kernel allocates file handles dynamically, but as yet it doesn't free them again.
內核能夠動態的分配文件句柄,但到目前爲止是不會釋放它們的

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內核能夠分配的最大文件句柄數。若是你看到了不少關於打開文件數已經達到了最大值的錯誤信息,你能夠試着增長該值的限制

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.已經分配的文件句柄數,2.已經分配單沒有使用的文件句柄數,3.最大文件句柄數。但在kernel 2.6版本中第二項的值總爲0,這並非一個錯誤,它實際上意味着已經分配的文件句柄無一浪費的都已經被使用了

file-max的默認值大概是系統內存的10%(系統內存以kb計算),

參考文章:http://www.mjmwired.net/kernel/Documentation/sysctl/fs.txt

相關文章
相關標籤/搜索