限制每个进程的物理内存 [英] Limit physical memory per process

查看:178
本文介绍了限制每个进程的物理内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个算法来执行一些外部内存计算,即您的输入数据不适合主内存,您必须考虑I / O复杂性。



因为对于我的测试,我不总是想使用实际输入我想限制我的进程可用的内存量。我发现,我可以设置 mem 内核参数限制所有进程的物理使用的内存(是正确的?)



是否有同样的方法,但每个进程限制 。我看到 ulimit ,但它只限制每个进程的虚拟内存。任何想法(也许我甚至可以通过编程从我的C ++代码中设置它)?

解决方案

你可以尝试'cgroups'。
要使用它们,以root身份键入以下命令。

 #mkdir / dev / cgroups 
# mount -t cgroup -omemory memory / dev / cgroups
#mkdir / dev / cgroups / test
#echo 10000000> /dev/cgroups/test/memory.limit_in_bytes
#echo 12000000> /dev/cgroups/test/memory.memsw.limit_in_bytes
#echo< PID> > / dev / cgroups / test / tasks

您要添加到的进程的PID cgroup。请注意,此限制适用于分配给此cgroup的所有进程的总和。



从这一刻起,进程限制为10MB的物理内存和12MB的物理内存+ swap。



该目录中还有其他可调参数,但具体列表将取决于您使用的内核版本。



你甚至可以创建限制的层次结构,只创建子目录。



当fork / exec时cgroup会被继承,所以如果你添加shell



请注意,您可以将cgroups安装在任何您想要的目录中,而不仅仅是/ dev / cgroups。 / p>

I am writing an algorithm to perform some external memory computations, i.e. where your input data does not fit into main memory and you have to consider the I/O complexity.

Since for my tests I do not always want to use real inputs I want to limit the amount of memory available to my process. What I have found is, that I can set the mem kernel parameter to limit the physically used memory of all processes (is that correct?)

Is there a way to do the same, but with a per process limit. I have seen ulimit, but it only limits the virtual memory per process. Any ideas (maybe I can even set it programmatically from within my C++ code)?

解决方案

You can try with 'cgroups'. To use them type the following commands, as root.

# mkdir /dev/cgroups
# mount -t cgroup -omemory memory /dev/cgroups
# mkdir /dev/cgroups/test
# echo 10000000 > /dev/cgroups/test/memory.limit_in_bytes
# echo 12000000 > /dev/cgroups/test/memory.memsw.limit_in_bytes
# echo <PID> > /dev/cgroups/test/tasks

Where is the PID of the process you want to add to the cgroup. Note that the limit applies to the sum of all the processes assigned to this cgroup.

From this moment on, the processes are limited to 10MB of physical memory and 12MB of pysical+swap.

There are other tunable parameters in that directory, but the exact list will depend on the kernel version you are using.

You can even make hierarchies of limits, just creating subdirectories.

The cgroup is inherited when you fork/exec, so if you add the shell from where your program is launched to a cgroup it will be assigned automatically.

Note that you can mount the cgroups in any directory you want, not just /dev/cgroups.

这篇关于限制每个进程的物理内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆