gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:51:54 24 4
gpt4 key购买 nike

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

此外,如果您可以提供一个限制进程内存使用的示例程序并显示以下场景,那将会很有帮助。

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

-谢谢

最佳答案

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.

关于c - Centos中如何限制进程可以使用的最大内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41509903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com