Memory leaks in a web application


Following is my post to the Django mailing list from yesterday. The response was that Django isn’t known to leak memory so there should be something off with matplotlib or rather that way I am using it.
Hopefully someone here could comment on what could be causing the leaks. Thanks in advance.


I am using matplotlib/pyplot on my site to dynamically generate PNG
plots. And I am experiencing dramatic memory leaks. Within 10-15
hits, my Apache process grows from 15-20M to 100M.

I am using Django 1.0.2-final, Apache 2.2.1, Python 2.4.3, matplotlib The leak happens under both Apache (with mod_wsgi 2.3) and
the development server. My OS is RHEL5.

Below is a simple code snippet that causes the leak. Please let me
know if I am doing something wrong or if there is a better way to
write this. Thanks.

from matplotlib import pyplot

def test_graph (request):

f = pyplot.figure()
ax = f.add_subplot(111)





          'upper right', shadow=True, fancybox=True)


ax.set_ylabel('Value ')

f.text(.5, 0.93, 'my title', horizontalalignment='center')

response = HttpResponse(content_type='image/png')

both ways causes a leak

f.savefig( response, format = 'png' )


canvas = FigureCanvas(f)


canvas = None

ax = None

f = None

return response