Linux文件句柄数调整
首先介绍下Linux系统中"一切都是文件"。
1. Linux系统文件句柄数概念
文件句柄(Windows)
文件描述符(Unix/Linux):file discriptor,fd。对于内核而言,所有打开的文件都是通过文件描述符引用,文件描述符是一个非负整数,变化范围是0~(OPEN_MAX-1)。
其中,OPEN_MAX解释为
The maximum number of files that a process can have open at any time. Must not be less than _POSIX_OPEN_MAX()
实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。
2. 查询Linux系统文件句柄数
# ulimit -a
core file size (blocks, -c)
data seg size (kbytes, -d) unlimited
scheduling priority (-e)
file size (blocks, -f) unlimited
pending signals (-i)
max locked memory (kbytes, -l)
max memory size (kbytes, -m) unlimited
open files (-n)
pipe size ( bytes, -p)
POSIX message queues (bytes, -q)
real-time priority (-r)
stack size (kbytes, -s)
cpu time (seconds, -t) unlimited
max user processes (-u)
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
3. 配置文件
单一程序(进程)级别的限制
/etc/security/limits.conf * soft nofile
* hard nofile
* soft nofile
* hard nofile 第一列表示域(domain),可以使用用户名(root等),组名(以@开头),通配置*和%,%可以用于%group参数。
第二列表示类型(type),值可以是soft或者hard
第三列表示项目(item),值可以是core, data, fsize, memlock, nofile, rss, stack, cpu, nproc, as, maxlogins, maxsyslogins, priority, locks, msgqueue, nie, rtprio.
第四列表示值.
系统级别的限制
man proc:
/proc/sys/fs/file-max
This file defines a system-wide limit on the number of open files for all processes. (See
also setrlimit(), which can be used by a process to set the per-process limit,
RLIMIT_NOFILE, on the number of files it may open.) If you get lots of error messages about
running out of file handles, try increasing this value: echo > /proc/sys/fs/file-max The kernel constant NR_OPEN imposes an upper limit on the value that may be placed in file-
max. If you increase /proc/sys/fs/file-max, be sure to increase /proc/sys/fs/inode-max to -
times the new value of /proc/sys/fs/file-max, or you will run out of inodes. /proc/sys/fs/file-nr
This (read-only) file gives the number of files presently opened. It contains three num-
bers: the number of allocated file handles; the number of free file handles; and the maximum
number of file handles. The kernel allocates file handles dynamically, but it doesn’t free
them again. If the number of allocated files is close to the maximum, you should consider
increasing the maximum. When the number of free file handles is large, you’ve encountered a
peak in your usage of file handles and you probably don’t need to increase the maximum.
4. 如何修改最大句柄数
单一进程级别
临时生效: ulimit -n 或者 echo ulimit -n >>/etc/profile
永久生效:修改 /etc/security/limits.conf 配置文件
系统级别
修改 /proc/sys/fs/file-max 文件内容
方法一:直接修改sys文件, echo > /proc/sys/fs/file-max ;
方法二:直接修改 /etc/sysctl.conf 文件增加或者修改配置项 fs.file-max = ,输入 #sysctl -p 使之生效;
方法三: sysctl -w fs.nr_open=
如果增加/proc/sys/fs/file-max,需要同步调整/proc/sys/fs/inode-max到新file-max值的3-4倍,否则会用尽inode资源。通常,file-max的值设置为内存(KB)大小的10%左右,即
grep MemTotal /proc/meminfo | awk '{printf("%d\n",$2/10)}'
5. 如何查询当前系统中文件句柄数使用情况
1)统计各进程打开句柄数:lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr
2)统计各用户打开句柄数:lsof -n|awk '{print $3}'|sort|uniq -c|sort -nr
3)统计各命令打开句柄数:lsof -n|awk '{print $1}'|sort|uniq -c|sort -nr
Linux文件句柄数调整的更多相关文章
- linux文件句柄数
1.问题阐述: too many open files:顾名思义即打开过多文件数. 不过这里的files不单是文件的意思,也包括打开的通讯链接(比如socket),正在监听的端口等等,所以有时候也可以 ...
- linux 文件句柄数查看命令
当你的服务器在大并发达到极限时,就会报出“too many open files”. 查看线程占句柄数ulimit -a 输出如下:core file size (blocks, -c) 0data ...
- Linux文件句柄数配置
1.单程序句柄数限制 查看配置的句柄数:ulimit -n cat /etc/security/limits.conf 参考配置: * soft nofile 655360* hard nofile ...
- 修改Linux文件句柄限制
1. 添加ulimit -HSn 655350 到/etc/profile 2. 配置生效 source /etc/profile 修改linux文件句柄数 分类: LINUX 2010-09 ...
- Linux下查看进程打开的文件句柄数和如何修改
修改文件句柄数在Linux下,我们使用ulimit -n 命令可以看到单个进程能够打开的最大文件句柄数量(socket连接也算在里面).系统默认值1024. 对于一般的应用来说(象Apache.系统进 ...
- linux系统下的用户文件句柄数限制
linux系统下的用户文件句柄数限制 文章来源:企鹅号 为什么要修改用户打开的文件数 系统默认单个进程可以打开1024个文件,对于一些应用如tomcat.oracle等,运行时经常open成千上万个文 ...
- 修改linux最大文件句柄数
大家知道在linux服务器大并发调优时,往往需要预先调优linux参数,其中修改linux最大文件句柄数是最常修改的参数之一. 在linux中执行ulimit -a 即可查询linux相关的参数,如下 ...
- Linux下查看进程打开的文件句柄数
---查看系统默认的最大文件句柄数,系统默认是1024 # ulimit -n ----查看当前进程打开了多少句柄数 # lsof -n|awk '{print $2}'|sort|uniq -c|s ...
- linux设置打开文件句柄数
介绍 在Linux下有时会遇到Socket/File : Can't open so many files的问题.其实Linux是有文件句柄限制的,而且Linux默认一般都是1024(阿里云主机默认是 ...
随机推荐
- python第二课——数据类型1
day02(上午)主要讲了进制问题,小编之前已经发过了 day02(下午): 1.数据类型: 分类: 1).整数型:int浮点型(小数):float布尔型(True/False):bool 2).字符 ...
- 看懂shebang吧,只需一点点shell知识,从此再也不犯强迫症
Python2: 开启一个terminal,输入下面命令: yshuangj@ubuntu:~$ vim helloA.py 在vim编辑器中,进入编辑模式(按i),输入下面的代码,然后退出编辑模式( ...
- Odoo作为后端时如何返回数据给webapp、移动端app
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9307315.html 使用jinja2渲染的页面,可以直接在调用template.render()时传递参数 ...
- 20155314 2016-2017-2 《Java程序设计》实验四 Android程序设计
20155314 2016-2017-2 <Java程序设计>实验四 Android程序设计 实验任务 基于Android Studio开发简单的Android应用并部署测试 了解Andr ...
- Python自动化之traceback
import traceback try: 11/a except Exception: b = traceback.format_exc() traceback.format_exc()会存储详细的 ...
- c++中内存拷贝函数(C++ memcpy)详解
原型:void*memcpy(void*dest, const void*src,unsigned int count); 功能:由src所指内存区域复制count个字节到dest所指内存区域. 说明 ...
- 初识Qt窗口界面
1.新建一个新的Qt Gui应用,项目名称随意,例如MyMainWindow,基类选择QMainWindow,类名为MainWindow. 2.项目建立后,双击mainwindow.ui文件,在界面的 ...
- 关于NRF52832能否被替代的详解
ULP无线系统级芯片 nRF52832是用于ULP无线应用的功能强大的多协议单芯片解决方案.它结合了业界性能最佳的Nordic最新无线收发器.ARM Cortex M4F CPU和512kB闪存及64 ...
- docker - kubernetes 网络(转)+ 架构图
1.host网络 连接到 host 网络的容器共享 Docker host 的网络栈,容器的网络配置与 host 完全一样.可以通过--network=host指定使用 host 网络.docker ...
- CAN总线实际运用分析问题。
组态设计 人机交互 上位机 分布式控制系统 下位机 (单片机/PLC) CAN总线用线缆 连接方式(手牵手,T型) CAN总线接地(大地) http://bbs.gongkon ...