【问题标题】:How to limit the maximum memory a process can use in Centos?如何限制进程在 Centos 中可以使用的最大内存?
【发布时间】:2017-05-21 11:08:33
【问题描述】:

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

另外,如果您可以提供一个示例程序来限制进程的内存使用,并展示以下可能会有所帮助的场景。

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

-谢谢

【问题讨论】:

  • 你想看看ulimit
  • 正如@alk所说,见this
  • Ulimit 为整个系统设置限制。但我想限制特定进程的内存使用量。在这种情况下我有什么选择?
  • @SandeshVeerapur:“Ulimit 为整个系统设置限制”是什么让你这么想的? ulimit 为从 shell ulimit 调用的实例启动的进程设置可用资源。

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


【解决方案1】:
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.

【讨论】:

    猜你喜欢
    • 2014-08-05
    • 2021-12-23
    • 2015-07-27
    • 2010-10-30
    • 2013-03-29
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多