on the importance of close

I recently spent some time trying to debug what I thought was a memory
leak in matplotlib. I had added tried all sorts of shenanigans to
make sure my memory was freed and the memory collected, but the leak
persisted. I had written code like

for i in somerange:
    fig = figure()
    del fig

I eventually realized/remembered why this fails: because pylab manages
figures behind the scenes (eg managing the current figure) there is a
dictionary in _pylab_helpers that retains a reference to the figure.
Calling close on the figure removes this reference, so all I really
needed to do was

for i in somerange:
    fig = figure()

and pylab handles the call to gc.collect.

Just a reminder to myself and you all, that even though it looks like
a fig is not being referenced in your local code, there are still
references to it in pylab so if you're using pylab you should balance
each call to figure with a call to close.