Django memory usage going up with every request
Solution 1
I'm afraid I haven't got any definite answers. Graham Dumpleton's tips were most helpfull, but unfortunately he didn't make an answer (just comments), so there is no way to accept his response.
Although I still haven't fully resolved the issue, here are some basic tips for other people having similar problems:
- Read Webfaction's documentation:
- Make sure the
DEBUG
setting is set toFalse
- Don't use
mod_python
, usemod_wsgi
- Make sure you use the most recent version od
mod_wsgi
(Webfaction tends to install older versions) - Don't use Django to serve static content
- Try running
mod_wsgi
in a daemon mode (Webfaction installs it in embedded mode by default) [thanks Graham Dumpleton for the tip] - If you run in embeded mode, you can specify "inactivity-timeout=[seconds]" option. It will restart the process after [seconds] of inactivity, helping with increased memory usage. Read this forum post for detailed instructions.
- This script will help you monitor your memory usage easier, and more precisely [thanks ClaudioA for the tip]
Solution 2
We had a similar issue on Webfaction, but it turned out it wasn't because of them at all. There's a bug in Django about high memory usage when using the sitemap with lots of elements: http://code.djangoproject.com/ticket/11572
When we removed the sitemap it doesn't shoot up 90 Mb at the time any longer. Just thought I should mention it since it took a long time for use to troubleshoot.
Solution 3
I have the same problems with webfaction.
The method I use, and which webfaction told me I should keep using, is run a cron job that checks the memory every 5 minutes or so, and restarts any apps that are getting out of control.
Out of 4 python apps on webfaction, I average 4 restarts per day.
Solution 4
I'd suggest not to guess.
Take a look at http://code.google.com/p/django-dowser/, it is a very useful app for detecting memory leaks and figuring out what parts of your code are responsible for memory consumption.
Solution 5
Check if the in-process memory cache backend is enabled, if yes, that could be the problem (new cache entries at each request).
Ludwik Trammer
Updated on July 02, 2022Comments
-
Ludwik Trammer almost 2 years
I moved my first Django project from DjangoEurope to Webfaction, and that started an issue looking like a memory leak. With every single request memory usage of the server process goes up about 500kb. It never goes down. This goes on until Webfaction kills it for using too much memory.
I can clearly see this when I refresh the Django's admin interface in my browser (although this happens with every single page, not only with admin interface - I though admin interface would be a nice test case, because there is no my code directly there). With every browser reload the memory usage goes up couple hundreds kilobytes.
I test the memory using a command suggested by Webfaction:
ps -u publica -o rss,etime,pid,command
More about my setup:
- Django 1.1 (stable)
- Default Webfaction Django setup using Apache and mod_wsgi
DEBUG
set toFalse
- MySQLdb 1.2.2 from Webfaction, but after hearing it had some problems I tried version 1.2.3c. Didn't help.
Edit: I created an empty Django project - default Django configuration plus
django.contrib.admin
and fresh empty database (tried both with mysql and postgresql). I started reloading Django admin in my browser and watched memory usage. At first I saw the problem occurring - memory usage grew after every reload. But then it stabilized and stopped growing. That's consistant with how my original project behaved on Django Europe. Unfortunately on Webfaction it never seems to stabilize (or at least not within limits of memory available to my account). Any advice? -
jathanism about 14 yearsSounds like something questionable with their environment if they're recommending to users to manually restart active processes.
-
Ludwik Trammer about 14 yearsI did start by carefully analyzing those tips. I do serve static files they way it is recommended (I checked at least five times during past two days ;]). I didn't know about the script. Thanks.
-
thomasguenzel about 14 yearsIf you just have one application running, it's pretty easy to avoid problems. I think that they actually do restart your app every once in a while anyway. But when you get four apps in a single account, then it's pretty easy to start hitting your limit -- especially when webfaction is stingy with memory, and won't let you buy more than 240 MB or so per account.
-
Graham Dumpleton about 14 yearsBut are you using daemon mode of mod_wsgi, or are you trying to run everything in embedded mode of mod_wsgi? Even with WebFaction config keeping Apache server child processes down to 2, if using embedded mode will still take more memory than daemon mode with one process for Django instance.
-
Ludwik Trammer about 14 years@Graham - thanks. I upgraded mod_wsgi to 3.1 (from 2.5 provided by webfaction) and that seemed to help a little (for the first time I saw instances of memory usage actually going down a bit). Then I switched to a daemon mode, but haven't noticed much difference. I feel a little intimidated by the configuration options. I'e got my
ServerLimit
set to "2",MaxRequestsPerChild
to "500",WSGIDaemonProcess
to "[mydomain] processes=2 inactivity-timeout=1800 threads=15" andWSGIRestrictEmbedded
turned on. Does this configuration make sense? -
Ludwik Trammer about 14 yearsOh, and could you post your suggestion about running in a daemon mode as separate answer (possibly with an example of sane values for configuration options)? I think it's the best answer so far and it's a shame it's buried in comments.
-
Claudio Acciaresi about 14 yearsMm.. didn't know that webfaction was limiting the memory that you can buy... thanks.
-
Graham Dumpleton about 14 yearsOne can use mod_python and mod_wsgi together fine, it isn't that mod_wsgi doesn't like it. You just loose a few configuration options for mod_wsgi as mod_python is responsible for initialisating Python, not mod_wsgi. Also, mod_python has memory leaks even if enabled in Apache but not used. That memory leak only becomes an issue if doing lots of Apache restarts and isn't something that occurs on every request.
-
Ludwik Trammer about 14 yearsIt isn't. I use memcached now (it uses it's own process, that grows separately), but turning off caching doesn't caching anything.