How can I restrict RAM usage for a particular user or process?

9,914

Solution 1

ulimit is your friend. Check out the -m flag. I think you set this before you launch your process and it applies from then on (in the current shell).

Solution 2

In addition to the shell commands ulimit (for bash) or limit (for csh), you can control this from inside your code with the ulimit() function.

Share:
9,914

Related videos on Youtube

guerda
Author by

guerda

Updated on September 17, 2022

Comments

  • guerda
    guerda over 1 year

    I'm using a background service that leaks memory from time to time. To prevent using the Alt+SysRq REISUB[1], I'd like to restrict the RAM usage for one process or a system user.

    I really would like to say: User x may use the maximum amount of RAM of 1 GB.

    Is this feasible? Do I need kind of virtual machine for this? I'm using Ubuntu 9.10.

  • Stephan202
    Stephan202 over 14 years
    Obligatory manpages: linux.die.net/man/1/ulimit (shell command) and linux.die.net/man/3/ulimit (C function).
  • guerda
    guerda over 14 years
    Thanks, nice hint! As I understand ulimit, it limits all users memory/process/whatever consumption. I just like to limit it to one user/process & subprocesses.
  • KeithB
    KeithB over 14 years
    ulimit is a shell command, and only affects processes started from the same shell after ulimit is executed. You startup the process in a shell script with a ulimit command to have it affect just that process.