title: Reducing Django Memory Usage. Low hanging fruit?
body: <p>My memory usage increases over time and restarting Django is not kind to users.</p>
  
  <p>I am unsure how to go about profiling the memory usage but some tips on how to start measuring would be useful.</p>
  
  <p>I have a feeling that there are some simple steps that could produce big gains. Ensuring 'debug' is set to 'False' is an obvious biggie.</p>
  
  <p>Can anyone suggest others? How much improvement would caching on low-traffic sites?</p>
  
  <p>In this case I'm running under Apache 2.x with mod_python. I've heard mod_wsgi is a bit leaner but it would be tricky to switch at this stage unless I know the gains would be significant.</p>
  
  <p>Edit: Thanks for the tips so far. Any suggestions how to discover what's using up the memory? Are there any guides to Python memory profiling?</p>
  
  <p>Also as mentioned there's a few things that will make it tricky to switch to mod_wsgi so I'd like to have some idea of the gains I could expect before ploughing forwards in that direction. </p>
  
  <p><b>Edit:</b> Carl posted a slightly more detailed reply here that is worth reading: <a href="https://stackoverflow.com/questions/488864/django-deployment-cutting-apaches-overhead">Django Deployment: Cutting Apache&#39;s Overhead</a></p>
  
  <p><b>Edit:</b> <a href="http://blog.dscpl.com.au/2009/03/load-spikes-and-excessive-memory-usage.html" rel="noreferrer">Graham Dumpleton's article</a> is the best I've found on the MPM and mod_wsgi related stuff. I am rather disappointed that no-one could provide any info on debugging the memory usage in the app itself though. </p>
  
  <p><b>Final Edit:</b> Well I have been discussing this with Webfaction to see if they could assist with recompiling Apache and this is their word on the matter:</p>
  
  <blockquote>"I really don't think that you will get much of a benefit by switching to an MPM Worker + mod_wsgi setup.  I estimate that you might be able to save around 20MB, but probably not much more than that."</blockquote>
  
  <p>So! This brings me back to my original question (which I am still none the wiser about). How does one go about identifying where the problems lies? It's a well known maxim that you don't optimize without testing to see where you need to optimize but there is very little in the way of tutorials on measuring Python memory usage and none at all specific to Django.</p>
  
  <p>Thanks for everyone's assistance but I think this question is still open!</p>
  
  <p><strong>Another final edit ;-)</strong></p>
  
  <p>I asked this on the django-users list and got some <a href="http://groups.google.com/group/django-users/browse_thread/thread/1d56d48fc192ceed?fwc=1&amp;pli=1" rel="noreferrer"><em>very</em> helpful replies</a></p>
  
  <p><strong>Honestly the last update ever!</strong></p>
  
  <p>This was just released. Could be the best solution yet: <a href="http://www.rkblog.rk.edu.pl/w/p/profiling-django-object-size-and-memory-usage-pympler/" rel="noreferrer">Profiling Django object size and memory usage with Pympler</a></p>