Linux – Preventing Django app from causing Linux box to run out memory

apache-2.2djangolinuxmemorymod-wsgi

One of my Linux web servers recently ran out of memory, hung itself in an OOM kill frenzy, and had to be rebooted. This is a 512 MB VPS, but it has been running without problems since it was set up months ago. How can I prevent this from happening again?

I have not been able to find out from the logs which process was the cause. The processes killed were mysqld followed by apache2. Based on what's running on the box, I think it's likely that something went wrong in one of the Django applications (running under apache and mod_wsgi). Would limit the stack size (with the stack-size option to wsgi) help? Is is possible to limit wsgi or python or apache's memory usage in general? (The answers to this question say no.)

Best Answer

Don't run your WSGI application in embedded mode with prefork MPM if you are, use daemon mode instead so number of processes limited. Read:

http://blog.dscpl.com.au/2009/03/load-spikes-and-excessive-memory-usage.html

In version 4.0 of mod_wsgi, so long as you are using daemon mode, you can restrict memory of individual processes if operating system supports it.