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(阿里云主机默认是 ...
随机推荐
- dotnet core入门
dotnet 命令 C:\Users\yshuangj\Desktop\dotnet>dotnet Usage: dotnet [options]Usage: dotnet [path-to-a ...
- (转)获取安卓iOS上的微信聊天记录、通过Metasploit控制安卓
在这篇文章中我们将讨论如何获取安卓.苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制.文章比较基础.可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对 ...
- 【转】打包2个10g文件 测试
微博上kevin_prajna提了一个问题:“求Linux下一打包工具,需求:能把两个10G的文件打包成一个文件,时间在1分钟之内能接受!”. 暂且作答一下吧.首先问题是求解工具,那么我们忽略IO问题 ...
- C#版谷歌地图下载器设计与实现
关于如何将地球经纬度坐标系统转换成程序中常用到的平面2D坐标系统,网上的文章很多,参考http://www.cnblogs.com/beniao/archive/2010/04/18/1714544. ...
- C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案
C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案: 1.首先试最常规的方法:Cle ...
- jdbc 和oracle数据库 建立连接
package jdbc; import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException; ...
- mapent
package test12; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import ja ...
- Kafka设计解析(一)Kafka背景及架构介绍
转载自 技术世界,原文链接 Kafka设计解析(一)- Kafka背景及架构介绍 本文介绍了Kafka的创建背景,设计目标,使用消息系统的优势以及目前流行的消息系统对比.并介绍了Kafka的架构,Pr ...
- 02_Docker在CentOS 6和CentOS 7下的安装
CentOS 7 环境下安装docker 安装Docker 检查系统内核是否高于Linux3.10版本 uname -r 使用root权限操作,确保yum包是最新版本 sudo yum update ...
- 各国货币json文件
[ {"countryname":"","name":"请选择","currency":" ...