matplotlib-0.60.2 - Memory Leaks

Hi, I was wondering if anybody else was seeing memory

    > leaks in the latest version (0.60.2).

    > I'm using the Agg backend to produce images for web
    > pages.

    > The release notes for 0.60.2 indicate that memory leaks
    > have been fixed. Are there any known leaks left?

Yes, when I say memory leaks were fixed, I mean I identified and fixed
some, but am not claiming that all are fixed. There are known memory
leaks. For example, there is a small leak in freetype2 that was fixed
in freetype CVS but may not be released yet. There appears to be a
leak in _backend_agg's write_png method, but I haven't traced this
sufficiently to find if it is in my code or in libpng or in zlib.

The whole memory leak situation has continued to improve as I've
migrated almost all of the extension code to pycxx, and I have a
number of unit tests to test leaks in various components. For
example, there are no detectable leaks in the transforms module.

Below is a prototypical script that I use to diagnose and report
memory leaks. Please use something like it when reporting leaks so I
get an idea of the magnitude of the problem (ie bytes per figure).
Also please provide your platform, matplotlib version, backend and as
much information about the versions of the libraries you are using:
freetype, png and zlib. It would also help if you posted code so I
could look for any obvious coding errors vis-a-vis matplotlib.

Running the code below, I'm getting about 400 bytes / per / figure
(matplotlib-0.60.2 / agg on linux with freetype 2.17, libpng1.2 and
libz 1.1.4). I know leaks of this size can cause troubles on an
application server, and in animated figures, and will continue to try
and track them down. As I noted above, some of this results from the
libraries on which matplotlib depends, and so may be system and
version dependent. If your numbers are much worse, it may be that you
are doing something wrong, ie, not properly closing, reusing figure,
etc.

Let me know what you find,
JDH

import os, sys, time
import matplotlib
matplotlib.use('Agg')
from matplotlib.matlab import *

def report_memory(i):
    pid = os.getpid()
    a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines()
    print i, ' ', a2[1],
    return int(a2[1].split()[1])

N = 100
for i in range(N):
    ind = arange(100)
    xx = rand(len(ind), 1)
    figure(1)
    plot(ind, xx[:,0])
    savefig('tmp%d.png' % i, dpi = 75)
    close(1)

    val = report_memory(i)
    if i==1: start = val

end = val
print 'Average memory consumed per loop: %1.4fk bytes\n' % ((end-start)/float(N))