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(阿里云主机默认是 ...
随机推荐
- 智能家居中的物联网网关的可信计算平台模块(TPM)设计
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/BlueCloudMatrix/article/details/24184461 摘要: 随着智能家居 ...
- cmdb截图
- css中位置计算
一.position属性 1. position的属性: ①absolute :绝对定位:脱离文档流的布局,遗留下来的空间由后面的元素填充.定位的起始位置为最近的已定位(设定了position)父元素 ...
- c++——深拷贝和浅拷贝
深拷贝和浅拷贝 默认复制构造函数可以完成对象的数据成员值简单的复制 对象的数据资源是由指针指示的堆时,默认复制构造函数仅作指针值复制 1浅拷贝问题 1.c++默认的拷贝构造函数 2.=号操作符 都是浅 ...
- USB耳机声卡-音频输入/输出控制器:DP108替代兼容CM108
DP108是一款完全替代CM108的高度集成的单芯片USB音频解决方案芯片.方便的USB即插即用的兼容性,用户可以快速创建易用性,高质量和便携式USB音频产品基于高度集成的单芯片解决方案.所有重要的模 ...
- 404 Note Found 队-Beta2
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- 拥抱.NET Core系列:MemoryCache 缓存域(转载)
阅读目录 MSCache项目 缓存域 写在最后 在上一篇“<拥抱.NET Core系列:MemoryCache 缓存选项>”我们介绍了一些 MSCache 的机制,今天我们来介绍一下 MS ...
- 【vue】vue依赖安装如vue-router、vue-resource、vuex等
方式一: 最直接的方式为在 package.json中添加如图依赖配置,然后项目 cnpm install即可 方式二: 根据vue项目的搭建教程,接下来记录下如何在Vue-cli创建的项目中安装vu ...
- 通过javascript修改class名字-学习笔记
<!doctype html> <html> <head> <meta charset="urtf-8"> <title> ...
- nginx负载均衡及配置
nginx负载均衡及配置 1 负载均衡概述 负载均衡由来是因为当一台服务器单位时间内的访问量很大时,此时服务器的压力也会很大,当超过自身承受能力时,服务器就会崩溃.为避免让服务器崩溃,用户拥有更好的体 ...