Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django memory usage going up with every request

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?

like image 965
Ludwik Trammer Avatar asked Feb 19 '10 00:02

Ludwik Trammer


4 Answers

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:
    • Reducing Memory Usage in general
    • Reducing mod_wsgi Memory Consumption
    • Reducing Django Memory Consumption
  • Make sure the DEBUG setting is set to False
  • Don't use mod_python, use mod_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]
like image 53
Ludwik Trammer Avatar answered Sep 23 '22 02:09

Ludwik Trammer


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.

like image 27
Emil Stenström Avatar answered Sep 22 '22 02:09

Emil Stenström


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.

like image 28
Ryan Ginstrom Avatar answered Sep 25 '22 02:09

Ryan Ginstrom


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.

like image 38
Mikhail Korobov Avatar answered Sep 24 '22 02:09

Mikhail Korobov