Release memory in a code using matplotlib?
Question: Well, I tried many things and I'm almost convinced that there is no way to
solve my problem. Here I go... I'm writing a simple software with tkinter and
in one part of this software I use matplotlib and basemaps to provide some
maps to the user. The problem is that in those maps are loaded a relative big
amount of data and in some computers it can be a problem if the user open many
maps. One part of mu code (the critical) is:

    
    
    def plota_bacia():  
        global status_shape,pontos,arq
        print status_shape 
        if status_shape == True:
        fig = pyl.figure(figsize=(12,8))
            fig.canvas.set_window_title('Bacia fornecida pelo arquivo: '+arq)
            fig.patch.set_facecolor('white')
            m = Basemap(projection='merc',llcrnrlat=-32.5,urcrnrlat=5.0,llcrnrlon=-65.0,urcrnrlon=-33.0,lat_ts=20,resolution='c')
            parallels = arange(-50.,20,0.5)
            meridians = arange(-90.,0.,0.5)
            ptos = []
            for x,y in zip(pontos[0],pontos[1]): 
                x1,y1=m(x,y)
                ptos.append((x1,y1))
            p = Polygon(ptos,facecolor='red',edgecolor='green',linewidth=1) 
            pyl.gca().add_patch(p) 
    
            pyl.title(arq)
            xmin,ymin = m(min(pontos[0])-0.5,min(pontos[1])-0.5)
            xmax,ymax = m(max(pontos[0])+0.5,max(pontos[1])+0.5)
            m.drawparallels(parallels,labels=[1,0,0,0],fontsize=16)
            m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=16)       
            m.readshapefile(dir_shape+'Brasil/BRASIL','r')
            pyl.xlim([xmin,xmax])
            pyl.ylim([ymin,ymax])
            pyl.show()
        else:tkMessageBox.showinfo( "Gráfico da bacia","Entre com uma bacia",parent=top)
    

Doing some tests I understood that the problem is way how python manages the
memory, for example:

    
    
    from pylab import *
    f = range(1,10000000,1)
    plot(f)
    show()
    del f ; gc.collect()
    

If I put the line "del f ; gc.collect()" after the second one ("f =
range(1,10000000,1)") I have some space released related to delete the
variable "f", but once I plot "f", I supose a matplotlib object is conected to
the part of the memory related to "f" and for this reason I can't release that
part of the memory. Is that correct? I tried cla(), clf(), close() and this
not help me. Sorry if I did some stupid, I program many things in python, but
I'm a environmental engineer, not a programmer. Thanks a lot!


Answer: After `pyl.show()` add `pyl.close(fig.number); del fig`.

