Limit amount of RAM to a process (Linux)
Solution 1
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.
Solution 2
The resource
module is the Python equivalent of ulimit
.
Solution 3
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.
Related videos on Youtube
Kc Tan
Long time Unix Sysadmin. Love to code in Python, especially Django. Writing code in Javascript can be fun as well.
Updated on September 17, 2022Comments
-
Kc Tan over 1 year
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
-
Kc Tan almost 14 yearsRight. 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...
-
Kc Tan almost 14 yearsLooks 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.
-
Daenyth almost 14 yearsAh, that makes sense then. Carry on.
-
Andras Balázs Lajtha almost 14 yearsLook 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.
-
Kc Tan almost 14 yearsThanks Joel for the link, I'll definately read through this article and give Monit a try!