I have a Zope server (just a long running python process) which tends to eat up all RAM on my server after a couple of days. I do not want to restart it every night - maybe it's better to limit the RAM for this process and see if it can work anyway.

So, how can I restrict this process to use not more than 256MB of RAM?

The Server is a 64bit Intel machine running Centos 5.3. I heard of ulimit to restrict a bash process, but how can I restrict it for a python process?


Thank you for your ideas! I am going to use a mixed approach for my problem:

  • I'll start the service with ulimit so it can only use 256MB RAM
  • Since I have my system monitored by Nagios, I'll use a nagios-check command to verify that my process is not using more than 192MB.
  • If my process uses more than 192MB, then a graceful restart will be initiated automatically.
  • If my process uses more than 220MB or the process does not run at all, an admin (myself) will be informed via SMS/E-Mail, so I will handle the problem manually.

That way I guess that I am on the safe side: automatic graceful restarts when needed (via nagios), and forced service stops (via ulimit) in case of a sudden peak in its RAM usage


ulimit is a hammer. It will hard kill the process when it hits the limit.

You might want something less aggressive. (something that does a graceful restart) Take a look at monit. http://mmonit.com/monit/

Here is an example config for zope. http://mmonit.com/wiki/Monit/ConfigurationExamples#zope

Look at using the mem or totalmem options for triggering your restart.

Best of luck.

| improve this answer | |
  • Looks interesting, but I am already using nagios (with nrpe etc.) and am quite happy with it. And I can not justify buying a license just for this specific issue I have. – mawimawi Jun 24 '10 at 4:50
  • Look closer. Monit it open source and free. Nagios is bigger scale. (Nagios will monitor hundreds of hosts) Monit runs locally on a host. See this: howtoforge.com/… for more implementation details. – Joel K Jun 25 '10 at 16:15
  • Thanks Joel for the link, I'll definately read through this article and give Monit a try! – mawimawi Jun 25 '10 at 21:08

The resource module is the Python equivalent of ulimit.

| improve this answer | |

The other responders seem to have your main question in hand, but to me what you're asking seems like treating the symptoms and not the cause. Why is the application leaking memory so badly? If you can track that down, the limiting is not really needed.

| improve this answer | |
  • Right. But I have no desire to debug a couple thousand lines of code (which I haven't wrote myself, but many different people) for a soon-to-be-replaced service... – mawimawi Jun 24 '10 at 4:45
  • Ah, that makes sense then. Carry on. – Daenyth Jun 24 '10 at 23:02

Since ulimit affects the shell and its subprocesses, if it's feasible you could try launching the Python process from inside another (ulimit-ed) shell.

Using Python that would be something like

subprocess.Popen('ulimit -v 262144; python_application', shell=True)
| improve this answer | |

Use Cgroups to limit the memory.

A cgroup limits memory to a configurable amount and is not a hard hammer like ulimit. It won't OOM kill the process. It will page out the rest of the memory needed by the process to disk.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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