如何限制进程在Centos中可以使用的最大内存?

时间:2017-01-06 16:27:14

标签: c memory memory-management centos7 resource-management

我想在Centos中限制进程的最大内存。可能存在这样的情况:进程最终使用所有可用内存或大部分内存影响系统中的其他进程。因此,我想知道如何限制它。

另外,如果您可以提供一个示例程序来限制进程的内存使用量,并显示以下有用的方案。

  1. 在设置限制内请求内存时内存分配成功。
  2. 当请求的内存超出设定限制时,内存分配失败。
  3. -Thanks

1 个答案:

答案 0 :(得分:0)

ulimit can be used to limit memory utilization (among other things)

Here is an example of setting memory usage so low that /bin/ls (which is larger than /bin/cat) no longer works, but /bin/cat still works.

$ ls -lh /bin/ls /bin/cat
-rwxr-xr-x 1 root root 25K May 24 2008 /bin/cat
-rwxr-xr-x 1 root root 88K May 24 2008 /bin/ls
$ date > test.txt
$ ulimit -d 10000 -m 10000 -v 10000
$ /bin/ls date.txt
/bin/ls: error while loading shared libraries: libc.so.6: failed to map segment from shared object: Cannot allocate memory
$ /bin/cat date.txt
Thu Mar 26 11:51:16 PDT 2009
$

Note: If I set the limits to 1000 kilobytes, neither program works, because they load libraries, which increase their size. above 1000 KB.

-d data segment size

-m max memory size

-v virtual memory size

Run ulimit -a to see all the resource caps ulimits can set.