35

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 to False
  • 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?

Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
  • 1
    If you are doing something leaky in your models it would certainly show up in the admin, so I'm not sure you've eliminated your code as culprit. For what it's worth, I've been running a Django application on Webfaction for over a year and have not had an issue like this. – Joe Holloway Feb 19 '10 at 05:36

8 Answers8

14

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:

Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
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.

Emil Stenström
  • 13,329
  • 8
  • 53
  • 75
1

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.

Ryan Ginstrom
  • 13,915
  • 5
  • 45
  • 60
  • 2
    Sounds like something questionable with their environment if they're recommending to users to manually restart active processes. – jathanism Feb 19 '10 at 15:11
1
Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
Claudio Acciaresi
  • 31,951
  • 5
  • 33
  • 43
  • I 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. – Ludwik Trammer Feb 19 '10 at 18:34
  • If 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. – Ryan Ginstrom Feb 21 '10 at 02:01
  • Mm.. didn't know that webfaction was limiting the memory that you can buy... thanks. – Claudio Acciaresi Feb 24 '10 at 19:24
  • First link is dead – jlandercy Mar 22 '23 at 07:49
1

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.

Mikhail Korobov
  • 21,908
  • 8
  • 73
  • 65
1

Check if the in-process memory cache backend is enabled, if yes, that could be the problem (new cache entries at each request).

Kedare
  • 1,289
  • 16
  • 31
  • It isn't. I use memcached now (it uses it's own process, that grows separately), but turning off caching doesn't caching anything. – Ludwik Trammer Feb 28 '10 at 10:12
0

I've had issues with memory on Webfaction too - they didn't really crop up until I added my fifth application though. I tried a few tweaks to my apache configs, but what finally worked for me was just switching over to mod_wsgi.

  • 2
    But 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. – Graham Dumpleton Feb 21 '10 at 05:09
  • @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" and `WSGIRestrictEmbedded` turned on. Does this configuration make sense? – Ludwik Trammer Feb 21 '10 at 13:30
  • Oh, 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. – Ludwik Trammer Feb 21 '10 at 13:35
0

Are both mod_python and mod_wsgi modules loaded into apache?

I know mod_wsgi doesn't like having mod_python in its building. Check its not loaded.

John Mee
  • 50,179
  • 34
  • 152
  • 186
  • 1
    One 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. – Graham Dumpleton Feb 26 '10 at 11:03