Memory leak when using pyplot.ion() ?

Hi,

I am having problems with a script. It runs a number of iterations and plots and saves a number of plots on each iteration. After the plots have been saved I issue the pyplot.close(‘all’) command so despite many plots
being created only 4 should be open at any given time which should not cause any memory problems. When I run the script however I see the RAM usage gradually growing without bound and eventually causing the script to crash. Interestingly I have found if I
comment out the pyplot.ion() and pyplot.ioff() the problem vanishes. So I do have a workaround but it would still be good to have this fixed.

My OS is Windows XP Service Pack 3

Python 2.6

Matplotlib 1.3.1

The code below is a stripped down version of my script which still exhibits the problem. Interestingly if I set the number of iterations such that it won’t run out of memory before the script completes then one final
call to pyplot.close(‘all’) will release all the memory. It almost looks like there is some sort of ‘race condition’ between the close(‘all’) at the end of one iteration and the plot commands in the next iteration which create new figures. In the taskbar
Windows shows the number of windows associated with each application => while this script runs I expect to see between 1 and 5 windows (interpreter + 1->4 figures), however as the script progresses and the memory usage grows Windows lists a growing number
of windows associated with Python (multiple instances of figure1/2/3/4).

Oisín.

-- coding: utf-8 --

import sys

import time

import numpy as np

from matplotlib import pyplot

import os

Main script body

try:

for gain in range(1,20,2):

for PortToTest in range(8):

dirname = ‘.\crash’

runname = ‘\P’ + str(PortToTest) + str(gain) + \

‘_’ + time.strftime(‘d%dh%Hm%Ms%S’)

dirname = dirname + runname

os.mkdir(dirname)

os.system('copy ’ + sys.argv[0] + ’ ’ + dirname )

nIts = 50

echoPlotDec = 10

ResidN = np.random.rand(4,100)

MaxSl = np.random.rand(4,100)

MaxOld = np.random.rand(4,100)

MaxNew = np.random.rand(4,100)

EchoA = np.random.rand(100,160)

pyplot.ion()

pyplot.figure()

pyplot.plot(np.transpose(ResidN),’.-’)

pyplot.savefig(dirname + ‘\results.png’,format=‘png’)

pyplot.figure()

pyplot.plot(np.transpose(EchoA[0:nIts-1:echoPlotDec,:]),’.-’)

pyplot.plot(np.transpose(EchoA[nIts:2nIts-1:echoPlotDec,:]),’-’)

pyplot.savefig(dirname + ‘\CoefsA.png’,format=‘png’)

pyplot.figure()

pyplot.plot(np.transpose(MaxOld),’.-’)

pyplot.plot(np.transpose(MaxNew),’*-’)

pyplot.savefig(dirname + ‘\MaxAbsA.png’,format=‘png’)

pyplot.figure()

pyplot.plot(np.transpose(MaxSl),’.-’)

pyplot.savefig(dirname + ‘\MaxAbsSl.png’,format=‘png’)

pyplot.close(‘all’)

except RuntimeError, msg:

print ‘Exception occurred in main script body’

print >>sys.stderr, msg

raise

finally:

print “Test done”

Display plots

pyplot.ioff()