How to close a figure ?

This is a previously reported problem, related to:

a) closing figures (garbage collection)
b) memory usage issues (and memory leaks, which depend on the selected backend)

I adapted the script just provided by John Hunter to show memory usage at each new figure:

import sys
import pylab
import os
import time

report_filename = 'memory_report_in_%s.txt' % pylab.matplotlib.get_backend()
fid = file(report_filename,'wt')
fid.write('Date/time of test = %s\n' % time.asctime())
fid.write('OS version = %s\n' % os.sys.version)
fid.write('OS platform = %s\n' % os.sys.platform)
fid.write('Matplotlib version = %s\n' % pylab.matplotlib.__version__)
fid.write('Matplotlib revision = %s\n' % pylab.matplotlib.__revision__)
fid.write('Matplotlib backend = %s\n' % pylab.matplotlib.get_backend())
      pylab.ion()
a=pylab.arange(0,10)

def report_memory():
    ### Attention: the path to the pslist utility should be adjusted according to installation!
    if os.sys.platform == 'win32':
        ps_exe_filename = os.path.join(os.getcwd(),'pslist.exe') #Build ps filename
        a = os.popen('%s -m python' % ps_exe_filename).readlines() #Build and execute command
        b = a[8]
        c = b.split()
        return int(c[3])
    else:
        print 'Sorry, you have to adapt the command for your OS!'
        return 0

while 1:
    fig = pylab.figure()
    ax = fig.add_subplot(111)
    x, y = pylab.nx.mlab.rand(2,30)
    ax.plot(x,y,'o')
    fig.canvas.draw()
    memory_usage=report_memory()
    k = raw_input("Current memory usage %d [kBytes]. Press any key to continue, q to quit: " % memory_usage)
    fid.write('%8d [kBytes]\n' % memory_usage)
    if k.lower().startswith('q'):
        fid.close()
        sys.exit()
    pylab.close()
show()

Results using TKAgg are given below:

Date/time of test = Tue Jun 06 16:32:02 2006
OS version = 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)]
OS platform = win32
Matplotlib version = 0.87
Matplotlib revision = $Revision: 1.122 $
Matplotlib backend = TkAgg
   21484 [kBytes]
   23688 [kBytes]
   25824 [kBytes]
   27952 [kBytes]
   30176 [kBytes]
   32300 [kBytes]
   34436 [kBytes]
   36660 [kBytes]
   38780 [kBytes] ....

Results using GTKAgg are much better (given below):

Date/time of test = Tue Jun 06 16:32:37 2006
OS version = 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)]
OS platform = win32
Matplotlib version = 0.87
Matplotlib revision = $Revision: 1.122 $
Matplotlib backend = GTKAgg
   29920 [kBytes]
   32896 [kBytes]
   34212 [kBytes]
   34240 [kBytes]
   34340 [kBytes]
   34456 [kBytes]
   34548 [kBytes]
   34580 [kBytes]
   34680 [kBytes]
   34776 [kBytes]
   34804 [kBytes]
   34912 [kBytes]
   35008 [kBytes]
   35104 [kBytes]
   35140 [kBytes]
   35244 [kBytes]
   35336 [kBytes]
   35392 [kBytes]
   34364 [kBytes]...

As suggested by Hunter some time ago, TkAgg is leaking (under Win32). If TkAgg is placed as the "default" backend for
Win32 users, maybe this problem should deserve some attention.
With GTKAgg (and also some other backends tested) everything seems to be fine!

Regards,
Clovis