大网数据广告活动
服务器运维问答

On Linux (CentOS 7.2), why does ulimit -m not appear to restrict memory usage?

· 52CE 我爱测

I have set ulimit -m 1000000 for my shell, then started a process that consumes a bunch of memory (a simple python script from https://unix.stackexchange.com/questions/99334/how-to-fill-90-of-the-free-memory ). I tell my python s…

问题原文

原帖链接 · Mike S · CC BY-SA 3.0

I have set ulimit -m 1000000 for my shell, then started a process that consumes a bunch of memory (a simple python script from https://unix.stackexchange.com/questions/99334/how-to-fill-90-of-the-free-memory). I tell my python script to consume 2G of memory and it appears to do so (this output from htop):

    Mem[|||||||||||||||||||||||||||||||||1742/1839MB]     Uptime: 137 days(!), 16:56:32
    Swp[|||||||||||||                     591/2047MB]

  PID  USER   PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command               
  6207 mikes  20   0  2123M 1625M   156 S  0.0 88.3  0:08.77 python ./consume_memory 2000

...Look at the "RES" column and the "MEM" graph above.

ulimit -a shows:

max memory size         (kbytes, -m) 1000000

...Why didn't my process fail to start? If I set my virtual memory size with ulimit -v 1500000, for example, my process fails as expected:

$ ./consume_memory 900
Traceback (most recent call last):
  File "./consume_memory", line 14, in <module>
    data = megabyte * count
MemoryError

System specs:

CentOS Linux release 7.2.1511 (Core) 
Linux hostname 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

已采纳解答

原帖链接 · meuh · CC BY-SA 3.0

In bash if we do command -V ulimit we find ulimit is a shell builtin, so we can do help ulimit which says

 -m        the maximum resident set size

so we are setting the RSS. To see what system call bash is actually using, we can try

$ strace bash -c 'ulimit -m 10000'
setrlimit(RLIMIT_RSS, {rlim_cur=10000*1024, rlim_max=10000*1024}) = 0

and then see in man setrlimit that

RLIMIT_RSS. Specifies the limit (in pages) of the process's resident set (the number of virtual pages resident in RAM). This limit has effect only in Linux 2.4.x, x < 30, and there affects only calls to madvise(2) specifying MADV_WILLNEED.

So it looks like it has no effect in Linux these days.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。