plt.close() not releasing memory? Or memory leak elsewhere?

Hi All.

I’m experiencing some memory issues with some python/matplotlib code. I have a large object/class containing a lot of data, and whilst using that object to create (about 30) graphs I keep running into memory errors. Here is a description of my problem:

Firstly, I am able to create each graph individually. So if I run each graph creation function individually in the interpreter I get the desired result:

MyClass.TypicalPlot(save=True, show = False)

However, if I create a new function to run each graph function in a script then I get a “MemoryError: Could not allocate memory for path”. (varies at the precise point where, but its normally after 3 or 4 graphs). I am able to run each plot function in an interpreter without issue, so I can only assume its some kind of memory/garbage collection issue.

def saveAllPlots(self, comments = False):

        if self.comment is None: comment = False
        else: comment = True
        self.TypicalPlot(save=True, show=False, comment=comment)
        self.AnotherPlot(save=True, show=False)
        self.AnotherPlot2(save=True, show=False)
        self.AnotherPlot3(save=True, show=False)
        ...etc, etc, etc

Here is what a typical plot function looks like in my code (they are all very similar)

def TypicalPlot(self, title=None, comment=False, save=False, show=True):

if title is None:
    title = self.dat.title

fig = plt.figure()
host = SubplotHost(fig, 111)
fig.add_subplot(host)
par = host.twinx()
host.set_xlabel("Time (hrs)")
host.set_ylabel("Power (W)")
par.set_ylabel("Temperature (C)")
p1, = host.plot(self.dat.timebase1, self.dat.pwr, 'b,', label="Power",
                markevery= self.skip)
p2, = par.plot(self.dat.timebase2, self.dat.Temp1, 'r,',
               label="Temp 1", markevery= self.skip)
p3, = par.plot(self.dat.timebase2, self.dat.Temp2, 'g,',
               label="Temp 2", markevery= self.skip)
p4, = par.plot(self.dat.timebase2, self.dat.Temp3, 'm,',
               label="Temp 3", markevery= self.skip)
host.axis["left"].label.set_color(p1.get_color())
# par.axis["right"].label.set_color(p2.get_color())
#host.legend(loc='lower left')
plt.title(title+" Temperature")

leg=host.legend(loc='lower left',fancybox=True)
#leg.get_frame().set_alpha(0.5)
frame  = leg.get_frame()
frame.set_facecolor('0.80')

### make the legend text smaller
for t in leg.get_texts():
    t.set_fontsize('small')

### set the legend text color to the same color as the plots for added
### readability
leg.get_texts()[0].set_color(p1.get_color())
leg.get_texts()[1].set_color(p2.get_color())
leg.get_texts()[2].set_color(p3.get_color())    
leg.get_texts()[3].set_color(p4.get_color())        

if show is True and save is True:
    plt.show()
    plt.savefig('temp.png')
elif show is True and save is False:
    plt.show()
elif show is False and save is True:
    plt.savefig('temp.png')
    plt.clf()
    plt.close(fig)

Has anyone come across this before?

Regards,

Sam Stevenson