深入明白file-max与ulimit的差别,以及file-max最大值和限定最大值的因素

分享
计算机软件开发 2024-9-8 19:49:27 72 0 来自 中国
Linux对可以或许打开的文件句柄的数目做了限定。限定是分为三个层面:
体系层面、用户层面、历程层面。
假如某个历程打开的文件句柄高出限定,再打开文件就会报错。
Too many open files 大概 Socket/File: Can’t open so many files因此,在编程时,假如历程要占用大量文件句柄的话,就要看看是否要调整最大可打开的文件句柄数,同时也要注意close开释掉不消的文件句柄。
查看文件句柄数限定

体系层面

[root@anolis ~]# cat /proc/sys/fs/file-max376093用户层面

# ulimit -n1024历程层面

ps -ef|grep javaroot      3146     1  3 09:54 ?        00:10:08 /usr/local/jdk1.8.0_11/bin/java -server -jar /usr/local/tomcat/qhdzs/qhd_private.jar --spring.profiles.active=testcat /proc/3146/limitsLimit                     Soft Limit           Hard Limit           Units     Max cpu time              unlimited            unlimited            seconds   Max file size             unlimited            unlimited            bytes     Max data size             unlimited            unlimited            bytes     Max stack size            8388608              unlimited            bytes     Max core file size        0                    unlimited            bytes     Max resident set          unlimited            unlimited            bytes     Max processes             15591                15591                processes Max open files            4096                 4096                 files     Max locked memory         65536                65536                bytes     Max address space         unlimited            unlimited            bytes     Max file locks            unlimited            unlimited            locks     Max pending signals       15591                15591                signals   Max msgqueue size         819200               819200               bytes     Max nice priority         0                    0                    Max realtime priority     0                    0                    Max realtime timeout      unlimited            unlimited            us Max open files            4096                 4096                 files这就是历程级别的打开文件句柄数目限定体系级限定

理论上体系内存有多少就可以打开多少的文件句柄,但是在现实中内核是会做相应的处置惩罚,一般最大打开文件数会是体系内存的10%(以KB来盘算),称之为体系级限定。这个数字可以通过 cat /proc/sys/fs/file-max 大概 sysctl -a | grep fs.file-max 下令查看。

更改体系级限定有临时更改和永久更改两种方式:

临时更改:

session断开大概体系重启后会规复原来的设置值。使用下令 sysctl -w fs.file-max=xxxx,此中xxxx就是要设置的数字。
永久更改:

vim编辑 /etc/sysctl.conf 文件,在反面添加 fs.file-max=xxxx,此中xxxx就是要设置的数字。保存退出后还要使用sysctl -p 下令使其收效。
用户级限定

同时为了控制每个历程斲丧的文件资源,内核也会对单个历程最大打开文件数做默认限定,即用户级限定。32位体系默认值一般是1024,64位体系默认值一般是65535,可以使用 ulimit -n 下令查看。

更改用户级限定也有临时更改和永久更改两种方式:

临时更改:

session断开大概体系重启后会规复原来的设置值。使用下令 ulimit -SHn xxxx 下令来修改,此中xxxx就是要设置的数字。
永久更改:

vim编辑 /etc/security/limits.conf 文件,修改此中的 hard nofile xxxx 和 soft nofile xxxx,此中xxxx就是要设置的数字。保存后退出。关于hard和soft的区别,参照下面参考链接中的第5个。
文件句柄数最大值和影响最大值的因素

在Linux下,体系全部可以或许打开的fd(文件句柄)总数为:/proc/sys/fs/file-max,取决于内存
# The file-max parameter# file-max参数The file-max file /proc/sys/fs/file-max sets the maximum number of file-handles that the Linux kernel will allocate. We generally tune this file to improve the number of open files by increasing the value of /proc/sys/fs/file-max to something reasonable like 256 for every 4M of RAM we have: i.e. for a machine with 128 MB of RAM, set it to 8192 - 128/4=32 32*256=8192.# file-max file /proc/sys/fs/file-max设置Linux内核将分配的最大文件句柄数目。我们通常通过将/proc/sys/fs/file-max的值增长到一个公道的值来调整这个文件,比如每4M内存增长256个文件;也就是说,对于一台有128MB内存的呆板,将其设置为8192       128M/4M=32个    32*256=8192.The default setup for the file-max parameter under Red Hat Linux is: "4096" To adjust the value of file-max to 128 MB of RAM, type the following on your terminal:# Red Hat Linux下file-max参数的默认设置是“4096”要将file-max的值调整为128MB 内存,必要在终端实验以下下令[root@deep] /#  echo "8192" >/proc/sys/fs/file-maxAdd the above commands to the /etc/rc.d/rc.local script file and you'll not have to type it again the next time your server reboots.# 将上述下令添加到/etc/rc.d/rc.local脚本文件中,下次服务器重新启动时,就不必再实验它了。Edit the /etc/sysctl.conf file and add the following line:编辑 /etc/sysctl.conf 文件并添加以下行:          # Improve the number of open files          fs.file-max = 8192You must restart your network for the change to take effect. The command to manually restart the network is the following:# 必须重启网络,以使得更改收效。重启网络的下令如下:          [root@deep] /# /etc/rc.d/init.d/network restart          Setting network parameters [ OK ] Bringing up interface lo [ OK ] Bringing up interface eth0 [ OK ] Bringing up interface eth1 [ OK ] When you regularly receive from your server a lot of messages with errors about running out of open files, you might want to raise this limit. The default value is 4096. A file server or web server needs a lot of open files# 当你经常从服务器收到大量关于打开文件不敷的错误消息时,就必要进步此限定,默认值为4096,文件服务器大概web服务器必要大量的打开文件。文献资料来自:http://www.faqs.org/docs/securing/chap6sec72.html
[root@anolis ~]# free -m              total        used        free      shared  buff/cache   availableMem:           3720         227        3235           8         257        3267Swap:          4043           0        40433720/4=930    930*256=238080这台服务器上的 fs.file-max参数的理论最大值可以设置为238080。用户级别限定/etc/security/limits.conf

对linux某个用户设置体系资源,我们都已经知道可以用ulimit下令来查看和设置。
limits.conf 文件现实是 Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so 的设置文件,而且只针对于单个会话。
格式如下:
username|@groupname type resource limit
username|@groupname:设置必要被限定的用户名,组名前面加@和用户名区别。也可以用通配符*来做全部用户的限定。
type:有 soft,hard 和 -,soft 指的是当前体系收效的设置值。hard 表明体系中所能设定的最大值。soft 的限定不能比har 限定高。用 - 就表明同时设置了 soft 和 hard 的值
resource:
core - 限定内核文件的巨细
date - 最大数据巨细
fsize - 最大文件巨细
memlock - 最大锁定内存所在空间
nofile - 打开文件的最大数目
rss - 最大恒久设置巨细
stack - 最大栈巨细
cpu - 以分钟为单位的最多 CPU 时间
noproc - 历程的最大数目
as - 所在空间限定
maxlogins - 此用户答应登录的最大数目
要使 limits.conf 文件设置收效,必须要确保 pam_limits.so 文件被到场到启动文件中。
查看 /etc/pam.d/login 文件中有:
session required /lib/security/pam_limits.so
临时地,实用于通过 ulimit 下令登录 shell 会话期间。
永久地,通过将一个相应的 ulimit 语句添加到由登录 shell 读取的文件之一(例如 ~/.profile),即特定于 shell 的用户资源文件;大概通过编辑 /etc/security/limits.conf。
何谓core文件,当一个步伐瓦解时,在历程当前工作目次的core文件中复制了该历程的存储图像。core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。  core文件是个二进制文件,必要用相应的工具来分析步伐瓦解时的内存映像。

体系默认core文件的巨细为0,以是没有创建。可以用ulimit下令查看和修改core文件的巨细。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-19 11:48, Processed in 0.171227 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表