Memory leaks in a web application

Hi,

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
0.98.5.2. 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)
ax.plot([1,2,3])

ax.fill_between([1,2,3],[1,2,

3],[1.1,2.1,3.1])

ax.grid(True)

ax.legend(['hello'],

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

ax.set_xlabel('Time')

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' )

OR

canvas = FigureCanvas(f)

canvas.print_png(response)

canvas = None

ax = None

f = None

return response