How do I limit the memory usage of a specific process in linux?

12,975

You'll want to use ulimit

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.

Share:
12,975

Related videos on Youtube

user20222
Author by

user20222

Updated on September 17, 2022

Comments

  • user20222
    user20222 over 1 year

    I've written a program for a class that my professor will be testing in various low memory environments to see how it behaves when the program runs out of memory. Is there a way I can simulate the execution in a low memory environment without creating a virtual machine?

  • user20222
    user20222 about 14 years
    is there something that does something similar on a single user basis? I don't have the permission to make system wide changes.
  • Dennis Williamson
    Dennis Williamson about 14 years
    @user20222: You can use ulimit at the command line or in a script in Bash.
  • user20222
    user20222 about 14 years
    I'm getting-- ulimit: max memory size: cannot modify limit: Operation not permitted
  • user20222
    user20222 about 14 years
    I talked to the sysadmin and I'm not sure what was wrong but he fixed the problem.
  • David Schwartz
    David Schwartz over 12 years
    Setting -m limits the use of physical RAM on systems that enforce it. Setting -v limits the use of virtual RAM on systems that enforce it.
  • Mikko Rantalainen
    Mikko Rantalainen about 4 years
    Note that -m does nothing on modern Linux kernels. You have to use cgroups to limit physical RAM usage these days. Unfortunately, the user interface to cgroups is still pretty bad.