天天看點

linux 修改最大檔案描述符

linux下最大檔案描述符的限制有兩個方面,一個是使用者級的限制,另外一個則是系統級限制。

通常我們通過終端連接配接到linux系統後執行ulimit -n 指令可以看到本次登入的session其檔案描述符的限制,如下:

$ulimit -n

1024

當然可以通過ulimit -SHn 102400 指令來修改該限制,但這個變更隻對目前的session有效,當斷開連接配接重新連接配接後更改就失效了。

如果想永久變更需要修改/etc/security/limits.conf 檔案,如下:

vi /etc/security/limits.conf

* hard nofile 102400

* soft nofile 102400

儲存退出後重新登入,其最大檔案描述符已經被永久更改了。

這隻是修改使用者級的最大檔案描述符限制,也就是說每一個使用者登入後執行的程式占用檔案描述符的總數不能超過這個限制。

它是限制所有使用者打開檔案描述符的總和,可以通過修改核心參數來更改該限制:

sysctl -w fs.file-max=102400

使用sysctl指令更改也是臨時的,如果想永久更改需要在/etc/sysctl.conf添加

fs.file-max=102400

儲存退出後使用sysctl -p 指令使其生效。

與file-max參數相對應的還有file-nr,這個參數是隻讀的,可以檢視目前檔案描述符的使用情況。

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計算),

本文轉自 hb_fukua 51CTO部落格,原文連結:http://blog.51cto.com/2804976/835557

繼續閱讀