歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> UNIX打開文件太多提示的原因

UNIX打開文件太多提示的原因

日期:2017/2/28 11:08:47   编辑:關於Unix


一,什麼情況下,會新建和打開文件:
1,A JVM opens many files in order to read in the classes required to run your application.
High volume applications can use a lot of files in many ways.
2,each new socket requires a file. Clients and Servers communicate via TCP sockets.
3,Each browser's http request consumes TCP sockets when a connection is established to a Server.
二,文件描述符的釋放:(文件描述符是由無符號整數表示的句柄。進程使用它來標識打開的文件)
1,在文件關閉或進程終止時被關閉的。
2,如果想重用某個文件描述符,必須關閉與之關聯的所有文件描述符(父進程和子進程:文件描述符可以繼承,可由子進程使用)。
3,TIME_WAIT 結束時,才會釋放 TCP 套接字文件描述符。
(在 Unix系統中, TIME_WAIT在kernel參數tcp_time_wait_interval中設置.在Windows中,這個參數定義在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters的 TcpTimedWaitDelay鍵值
中. 默認值是240秒 (tcp_time_wait _interval 和 TcpTimedWaitDelay))
4,打開新文件時將會重用關閉的文件描述符
三,查看文件描述符的方法:
1,在UNIX平台,使用“文件列表” (lsof) 工具顯示有關打開的文件和網絡文件描述符的信息。
針對特定的process ,語句就是: lsof -p
這個命令可以在異常發生後檢查打開文件的最大數,你也可以通過lsof –h 顯示相應的語法和選項。
2, 在WINDOWS平台,使用handle 工具報告有關打開文件句柄的信息。
推薦使用Process Explorer 工具查看運行的進程和打開的文件列表.(google查一下)
四, 解決之道:
1,對於引發異常的進程,請定期通過工具或命令檢查該進程打開的文件和/或連接。
2,將檢查結果與操作系統對進程和總體的文件描述符限制進行比較。
3,根據檢查結果和實際需要提高 OS 限制來彌補文件描述符不足。並使TIME_WAIT 期間縮短為一個切合 實際的值。
注意事項:
1,如果未正確關閉文件,文件描述符可能未被釋放。
2,如果子進程使用的描述符未關閉文件,將不能重用該文件描述符。(繼承父進程文件描述符)
3, TIME_WAIT 結束前,TCP 套接字會使文件描述符保持打開狀態。
4, 請不要依賴 GC 和對象清除功能來釋放文件描述符
五, 如何修改文件描述符:
操作系統資源限制控制:
可以使用的文件描述符總數
單個進程最多可以打開的描述符數。
例如:
1,Windows: 通過文件句柄,打開文件句柄設置為 16,384.
2,Solaris: rlim_fd_cur 和 rlim_fd_max
/usr/bin/ulimit 實用程序定義允許單個進程使用的文件描述符的數量。 它的最大值在 rlim_fd_max 中定義,在缺省情況下,它設置為 65,536。 只有ROOT用戶才能修改這些內核值。
3,HPUX: nfile, maxfiles 和 maxfiles_lim
nfile 定義打開文件的最大數量。 此值通常由以下公式來確定: ((NPROC*2)+1000),其中 NPROC 通常為: ((MAXUSERS*5)+64)。 如果 MAXUSERS 等於 400,則經過計算得到此值為 5128。 通常可以將此值設高一些。maxfiles 是每個進程的軟文件極限,maxfiles_lim 是每個進程的硬文件極限。
4,Linux: nofile 和 file-max
soft nofile 1024
hard nofile 4096
設置命令如下:
echo 4096 > /proc/sys/fs/file-max
echo 16384 > /proc/sys/fs/inode-max
5,AIX: OPEN_MAX
文件描述符極限在 /etc/security/limits 文件中設置,它的缺省值是 2000。 此極限可以通過 ulimit 命令或 setrlimit 子例程來更改。 最大大小由 OPEN_MAX 常數來定義。
查看最大打開文件數
[root@localhost ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 81920
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

查看某一個進程(JAVA)打開的文件數:
[root@localhost ~]# ps -ef | grep java
root 4589 4556 0 13:55 pts/1 00:00:00 grep java

[root@localhost ~]# lsof -p 2992|wc -l
467

更改最大打開文件數:


#
#Each line describes a limit for a user in the form:
#
#
#
#Where:
# can be:
# - an user name
# - a group name, with @group syntax
# - the wildcard *, for default entry
# - the wildcard %, can be also used with %group syntax,
# for maxlogin limit
#
# can have the two values:
# - "soft" for enforcing the soft limits
# - "hard" for enforcing hard limits
#
# can be one of the following:
# - core - limits the core file size (KB)
# - data - max data size (KB)
# - fsize - maximum filesize (KB)
# - memlock - max locked-in-memory address space (KB)
# - nofile - max number of open files
# - rss - max resident set size (KB)
# - stack - max stack size (KB)
# - cpu - max CPU time (MIN)
# - nproc - max number of processes
# - as - address space limit
# - maxlogins - max number of logins for this user
# - priority - the priority to run user process with
# - locks - max number of file locks the user can hold
#
#
#
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
# End of file
Copyright © Linux教程網 All Rights Reserved