How do I limit the memory usage of a specific process in linux?
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.
Related videos on Youtube
user20222
Updated on September 17, 2022Comments
-
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 about 14 yearsis there something that does something similar on a single user basis? I don't have the permission to make system wide changes.
-
Dennis Williamson about 14 years@user20222: You can use ulimit at the command line or in a script in Bash.
-
user20222 about 14 yearsI'm getting-- ulimit: max memory size: cannot modify limit: Operation not permitted
-
user20222 about 14 yearsI talked to the sysadmin and I'm not sure what was wrong but he fixed the problem.
-
David Schwartz over 12 yearsSetting -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 about 4 yearsNote that
-m
does nothing on modern Linux kernels. You have to usecgroups
to limit physical RAM usage these days. Unfortunately, the user interface tocgroups
is still pretty bad.