Linux Limit For Resources

今天遇到一个报错:

max file descriptors [65535] for  process is too low, increase to at least [65536]
max number of threads [1024] for user myersguo is too low, increase to at least [2048]
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

这里我们要关注以下几个文件:

我们先来看这个文件/etc/security/limits.conf :

# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - an user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#
#<domain>      <type>  <item>         <value>
#

Soft Limits & Hard Limits

Oracle上看到的一个解释,超级棒:

You can set both soft and hard limits. The system will not allow a user to exceed his or her hard limit. However, a system administrator may set a soft limit (sometimes referred to as a quota) which can be temporarily exceeded by the user. The soft limit must be less than the hard limit.

Once the user exceeds the soft limit, a timer begins. While the timer is ticking, the user is allowed to operate above the soft limit but cannot exceed the hard limit. Once the user goes below the soft limit, the timer gets reset. However, if the user\s usage remains above the soft limit when the timer expires, the soft limit is enforced as a hard limit. By default, the soft limit timer is seven days.

即,soft limit限制超过后,会有一个计时器,计时器结束后,仍然超过,就KILL,否则OK。

三个限制

max file descriptor

maximum number of open files

文件描述符(file descriptor)是资源(文件,输入输出流,网络socket连接,etc)的标识。没打开一个文件描述符,系统都要分配一定内存,因此必须限制文件描述符的数量。

查看进程ID 3020的文件描述符:

lsof -p 3020  | wc -l
ls -a /proc/3020/fd  | wc -l

更新/etc/security/limits

 *       soft    nofile      65536
 *       hard    nofile      65536

这样就解决了file descriptor的限制。

ulimit -a 查看一下

*       soft    nproc       2048
*       hard    nproc       2048

或者:ulimit -u 2048设置每个用户最大进程数(max user process).

同样的设置虚拟内存:

sysctl -w vm.max_map_count=300000
cat /proc/sys/vm/max_map_count

我们可以从cat /proc/1920/maps | more来查看内存映射。

遇到的问题是已经解决了,但仿佛留给了我们更多的疑问。

微信公众账号:myersguo

参考资料

https://docs.oracle.com/cd/E19455-01/805-7229/sysresquotas-1/index.html
https://en.wikipedia.org/wiki/File_descriptor
http://duartes.org/gustavo/blog/post/how-the-kernel-manages-your-memory/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.那是一个炎热的夏季,太阳如火。岑念翘了班,连夜去见沈立扬。只因为他的一句:我回来了,有时间吗,一起吃顿饭。 她...
    狐文奚阅读 4,381评论 8 6
  • 一般APP含有详情页,目前主流做法,上面放个WKWebView(iOS8之后),下面原生拼接(评论或展示的Cell...
    Sui_H阅读 9,976评论 3 3
  • 作者:amnesiac 首发:官方论坛之中文版 字符串处理是脚本中最常见的一种操作,诚然,在这种操作中正则表达式的...
    amnesiac阅读 3,523评论 0 1

友情链接更多精彩内容