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(阿里云主机默认是 ...
随机推荐
- 解决Visual Studio 2015启动慢的问题
总发现vs2015经常把cpu给占满了,导致电脑卡的不要不要的.这是CodeLens引起的,因为装了VAssistX后,感觉CodeLens还没VAssistX好使.所以,关了CodeLens就可以了 ...
- POJ Football Game 【NIMK博弈 && Bash 博弈】
Football Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 451 Accepted: 178 Descr ...
- Node.js框架之Egg.js
Node.js是我前段时间接触的一个JavaScript的服务端语言,感觉还是挺有意思的. 也许有人说,你学这么多,学的过来吗?或者说学的太多,专而不精,有必要这样吗? 其实,我个人认为,自从我进入I ...
- gulp合并压缩
1.文件合并压缩 var concat = require(‘gulp-concat’); //引用 var uglify = require(‘gulp-uglify’); 接下来,只要conca ...
- JavaScript里的创建对象(一)
一.序 面向对象有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.ECMA-262把对象定义为“无序属性的集合,其属性可以包含基本值.对象或者函数”. 使用Obje ...
- 如何解析json字符串及返回json数据到前端
前言:最近需要实现的任务是:写若干个接口,并且接口中的请求数据是json格式,然后按照请求参数读取前端提前整理好的json数据,并且将json数据返回到服务器端. 主要的工具:Gson 2.8.2 ...
- unlink与close关系
close和unlink.以前时候总是不太理解两者的区别,最近看到一篇博客比较详细地描述了二者的本质区别,这里我引用了它的原文. “每一个文件,都可以通过一个struct stat的结 ...
- 20145209刘一阳《JAVA程序设计》课堂测试总结
20145209刘一阳<JAVA程序设计>课堂测试总结 这次重新学习JAVA这门课,我对本学期没有参与的测试进行了总结并制作成二维码方便老师检查,详细内容如下: 第一周课堂测试### ht ...
- 【LG3246】[HNOI2016]序列
[LG3246][HNOI2016]序列 题面 洛谷 题解 60pts 对于每个位置\(i\),单调栈维护它往左第一个小于等于它的位置\(lp_i\)以及往右第一个小于它的位置\(rp_i\). 那么 ...
- P2939 [USACO09FEB]改造路Revamping Trails
P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...