Resident Set Size (RSS) limit has no effect

自古美人都是妖i 提交于 2019-11-28 19:34:49

Form the getrlimit manpage:

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

It seems this is just not supported on Linux kernel 2.6.

Justin L.

You can accomplish this using cgroups. The long version is on my blog, but the short version (tested on Ubuntu 11.04) is:

  • Install the cgroup-bin package.

  • Edit /etc/cgconfig.config and create a group with limited memory. For instance, I added:

    group limited {
      memory {
        memory.limit_in_bytes = 50M;
      }
    }
    
  • Run

    $ sudo restart cgconfig
    $ sudo chown -R jlebar /sys/fs/cgroup/memory/limited
    $ cgexec -g memory:limited your/program
    

I observed my process with an RSS of 93M when I asked it to use only 50M, but that wasn't a problem for me, since my goal was just to get the program to page.

cgclassify lets you attach restrictions to a running process too. Note for RSS this only applies to memory allocated after the restriction comes into effect.

Ilia Barahovski

A related limit - virtual memory or address space (RLIMIT_AS) - does work. This allows limiting the python process and subprocesses memory without external tools.

>>> size = 50*1024*1024 # In bytes
>>> resource.setrlimit(resource.RLIMIT_AS, (size, resource.RLIM_INFINITY))
>>> a = 'a' * size
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError

From the man page:

RLIMIT_AS. The maximum size of the process's virtual memory (address space) in bytes.

Here is a good explanation of the difference between the Resident Set and the VM size - What is RSS and VSZ in Linux memory management.

JanKanis

I created a script to limit memory usage using cgroups and cgroup manager, useable for ad-hoc commands and not needing root privileges. See https://unix.stackexchange.com/questions/134414/how-to-limit-the-total-resources-memory-of-a-process-and-its-children/174894#174894

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!