Limit physical memory per process

后端 未结 6 2155
独厮守ぢ
独厮守ぢ 2021-02-02 14:19

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.

6条回答
  •  梦谈多话
    2021-02-02 14:44

    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  > /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.

提交回复
热议问题