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?
Update:
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
The
resource
module is the Python equivalent ofulimit
.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.
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.
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
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.