Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I need to test a process for memory management.

  • I do not have the source, so I need to do all the testing from the OS side.
  • I want to be able to say something like limitmemory 400k -p <pid>

Is there a way to do this in unix? Any common unix utility would be excellent.

share|improve this question

marked as duplicate by roaima, G-Man, Sparhawk, garethTheRed, vonbrand Oct 25 '15 at 13:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Some Unixes (Solaris, for example) do support more fine-grained control of resources on processes, process groups, project groups, etc. But I mention this only as a comment because you've tagged this 'linux' although you say 'in unix'. – jrg Sep 2 '10 at 20:11
Your question is extremely vague about what you actually want to do. Do you want to limit the amount of address space the process can use? Do you want to limit how many physical pages of memory it can have resident? – David Schwartz Oct 23 '11 at 6:01
Consider running it under e.g. valgrind. – vonbrand Oct 25 '15 at 13:21

ulimit -v, it's a shell builtin, but it should do what you want.

I use that in init scripts sometimes:

ulimit -v 128k
ulimit -v unlimited

It seems however, that you want ways of manipulating the maximum allocatable memory while the program is running, is that correct? Probably something like renice for manipulating the Priority.

There is, however, no such tool to my knowledge.

share|improve this answer
but ulimit will change the resource limit to all the process strted by that shell not just on pid. it limits the use of system-wide resources. – Hemant Aug 31 '10 at 16:28
@Hemant: then ulimit is not going to be as useful as I thought. Is there some other option? – Lazer Aug 31 '10 at 18:44
@Lazer: start the program with something like (ulimit -v 400; exec myprogram) or bash -c 'ulimit -v 400; myprogram'. – Gilles Aug 31 '10 at 22:58
@Lazer: please check Gilles comment. – Hemant Sep 1 '10 at 10:30
128k does not work on Bash 4.3.11 . But it is not needed either as it is in multiple of 1024 by default, so 128 should be the same. – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Oct 24 '15 at 20:35

I'm not very sure about this, but you could also use cgroups to limit the memory usage. The advantage of cgroups is that you can control processes that are already running. By the way systemd will use cgroups to control the system services.

Unfortunately I've experimented a bit and they don't seem to work very well on my Fedora 13 system.

share|improve this answer
cgroups are the Right Way Forward for the future. Won't work in older distributions, but from basically now onward, it's the way forward. Look into libcgroup sourceforge.net/projects/libcg for utilities to control them. – mattdm Nov 26 '10 at 15:09
The package is named libcgroup-tools on Fedora 17. – Cristian Ciupitu Jul 13 '12 at 3:21

To set the limit when starting the program, use ulimit -v 400, as indicated by polemon. This sets the limit for the shell and all its descendants, so in a script you might want to use something like (ulimit -v 400; myprogram) to limit the scope.

If you need to change the limit for a running process, there's no utility for that. You have to get the process to execute the setrlimit system call. This can often be done with a debugger, although it doesn't always work reliably. Here's how you might do this with gdb (untested; 9 is the value of RLIMIT_AS on Linux):

gdb -n -pid $pid -batch -x /dev/stdin <<EOF
call setrlimit(9, {409600, -1})
share|improve this answer

On Linux systems with kernel >=2.6.36 and util-linux >=2.21, you can use the prlimit command to set a process resource limits:

prlimit --rss=400000 --pid <pid>
share|improve this answer

There is the setrlimit() function, which allows to configure a process' limits in C. Write a C program to call setrlimit then to exec the command you want to be limited. setrlimit cannot change other processes' limits.

Luckily someone already wrote something similar. It can be downloaded from freshmeat. I had a quick look at the source code and it seems to be fine. Use rlimit at your own discretion. Note that rlimit also cannot change other processes' limits.

Edit: Gilles proposed a nice hack with gdb: Attach to the process with gdb then make the process call setrlimit. This would perhaps solve the problem to limit an already running process.

share|improve this answer

If you're using systemd, you can set some additional options in a .service file. The complete list of the options you can set is described here.

Below is a short example that shows how to use this feature of systemd:

# cat /etc/systemd/system/qbittorrent-nox.service
After=media-Kabi.mount network-online.target

Type= simple


Of course, you don't need all of the options I used. If you just want to limit memory usage, add MemoryLimit=50M, which limits to 50MiB.

And this is the result:

# systemctl status qbittorrent-nox.service
● qbittorrent-nox.service - qbittorrent-nox
   Loaded: loaded (/etc/systemd/system/qbittorrent-nox.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2015-05-07 19:25:20 CEST; 1s ago
     Docs: man:qbittorrent-nox
 Main PID: 21712 (qbittorrent-nox)
   Memory: 9.4M (limit: 50.0M)
   CGroup: /p2p.slice/qbittorrent-nox.service
           └─21712 /usr/bin/qbittorrent-nox

May 07 19:25:20 morfikownia systemd[1]: Started qbittorrent-nox.
May 07 19:25:20 morfikownia systemd[1]: Starting qbittorrent-nox...

So far, this works only for system daemons/services, and you can't limit, for instance, a firefox processes as a regular user in this way. But maybe this will change some day.

share|improve this answer

If you just want to test and measure the memory usage of your program please look at time. You can measure the resource usage of your program in many aspects including the terms of CPU Time and memory usage. Following command will give you the memory usage and CPU time usage of myProgram:

/usr/bin/time myProgram

(Be sure to give the absolute path to distinguish it from bash built-in time command.)

If you just want to limit the resources of your process, i recommend you to create a test user for this specific task. Limit the resources of this user according to your needs and run the process by the user. It seems that, in *nix world, resource management based on users is much more advanced than resource management based on processes.

You can check /etc/security/limits.conf to limit the resources of a user. Or you can use ulimit after logged in with the to-be-limited user.

share|improve this answer
On some versions of Unix/Linux, this isn't going to be helpful. (The man pages says this might be the case.) On a CentOS 5.5 box, I get "0" for maxresident every time. However, it does work on Fedora 14. – mattdm Nov 26 '10 at 15:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.