Limit physical memory per process

后端 未结 6 2156
独厮守ぢ
独厮守ぢ 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 <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.

    0 讨论(0)
  • 2021-02-02 14:46

    I would use the setrlimti with the RLIMIT_AS parameter to set the limit of virtual memory (this is what ulimit does) and then have the process use mlockall(MCL_CURRENT|MCL_FUTURE) to force the kernel to fault in and lock into physical RAM all the process pages, so that amount virtual == amount physical memory for this process

    0 讨论(0)
  • 2021-02-02 14:49

    Kernel mem= boot parameter limits how much memory in total OS will use.

    This is almost never what user wants.

    For physical memory, there is RSS rlimit aka RLIMIT_AS.

    0 讨论(0)
  • 2021-02-02 14:49

    As other posters have indicated already, setrlimit is the most probable solution, it controls the limits of all configurable aspects of a process environment. Use this command to see these individual settings on your shell process:

    ulimit -a
    

    The ones most pertinent to your scenario in the resulting output are as follows:

    data seg size           (kbytes, -d) unlimited
    max locked memory       (kbytes, -l) 64
    max memory size         (kbytes, -m) unlimited
    virtual memory          (kbytes, -v) unlimited
    

    Checkout the manual page for setrlimit ("man setrlimit"), it can be invoked programmatically from your C/C++ code. I have used it to good effect in the past for controlling stack size limits. (btw, there is no dedicated man page for ulimit, it's actually an embedded bash command, so it's in the bash man page.)

    0 讨论(0)
  • 2021-02-02 14:53

    have you considered trying your code in some kind of virtual environment? A virtual machine might be too much for your needs, but something like User-Mode Linux could be a good fit. This runs a linux kernel as a single process inside your regular operating system. Then you can provide a separate mem= kernel setting, as well as a separate swap space to make controlled experiments.

    0 讨论(0)
  • 2021-02-02 14:54

    I can't provide a direct answer but pertaining to doing such stuff, I usually write my own memory management system so that I can have full control of the memory area and how much I allocate. This is usually appliacble when you're writing for microcontrollers as well. Hope it helps.

    0 讨论(0)
提交回复
热议问题