Memory usage

matplotlib does cache some information, mainly text and font

    > stuff) between draws which can cause memory to grow, but
    > usually this is a small amount. I'll explore further.

I wrote a little GUI neutral script to expose the same functionality.
This helps test whether the problem is backend specific or not. If
you click anywhere in the axes, the sine wave updates.

I see the same effect: changing the sine wave frequency causes memory
usage to increase with each draw, but only for the first 50 or so
draws. This appears to be an agg thing, since I see it on GTKAgg,
TkAgg, WXAgg but not GTK or WX.

However, I don't believe it is a memory leak. Agg may be caching some
path or stroke information, because as I say the memory only seems to
increase when the path is changed. However, after 50 or so draws, the
increase stops, so it does not appear to be a memory leak. I often
see these kinds of effects when debugging memory leaks, and typically
discard the first 30-50 draws before assessing whether there is a leak
or not.

Here's the backend neutral script I am using, you can run it with
-dWXAgg, -dWX, -dGTKAgg etc to test various backends. Note that I
also explicitly clear the text cache with Text.cached = {}

JDH

#!/usr/bin/env python
import os, sys, time, gc
from matplotlib.text import Text
from pylab 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])

fig = figure()
ax = fig.add_subplot(111)
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
ax.plot(t,s)

def click(event):
    fig.clear()
    s = sin(2*pi*t*(click.cnt+1))
    ax = fig.add_subplot(111)
    ax.plot(t,s)
    gc.collect()
    Text.cached = {}
    report_memory(click.cnt)
    fig.canvas.draw()
    click.cnt += 1
click.cnt = 1

#connect('motion_notify_event', report)
connect('button_press_event', click)
show()