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 in case I forget about it in future and loose another weekend’s work.
My OS is Windows XP Service Pack 3
Python 2.6
Matplotlib 1.0.1
The code below is a stripped down version of my script which still exhibits the problem.
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’
f = open(dirname + ‘\results.m’,‘w’)
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
Decimate data for plotting if many iterations are run
if(nIts>10):
echoPlotDec = 10
else:
echoPlotDec = 1
ResidN = np.zeros((4,2*nIts))
MaxSl = np.zeros((4,2*nIts))
MaxOld = np.zeros((4,2*nIts))
MaxNew = np.zeros((4,2*nIts))
EchoA = np.zeros((2*nIts,160))
for kk in range(2*nIts):
ResidN[0,kk] = np.random.rand(1,1)
ResidN[1,kk] = np.random.rand(1,1)
ResidN[2,kk] = np.random.rand(1,1)
ResidN[3,kk] = np.random.rand(1,1)
MaxSl[0,kk] = np.random.rand(1,1)
MaxSl[1,kk] = np.random.rand(1,1)
MaxSl[2,kk] = np.random.rand(1,1)
MaxSl[3,kk] = np.random.rand(1,1)
MaxOld[0,kk] = np.random.rand(1,1)
MaxOld[1,kk] = np.random.rand(1,1)
MaxOld[2,kk] = np.random.rand(1,1)
MaxOld[3,kk] = np.random.rand(1,1)
MaxNew[0,kk] = np.random.rand(1,1)
MaxNew[1,kk] = np.random.rand(1,1)
MaxNew[2,kk] = np.random.rand(1,1)
MaxNew[3,kk] = np.random.rand(1,1)
EchoA[kk,:] = np.random.rand(1,160)
f.close()
pyplot.ion()
pyplot.figure()
pyplot.hold(True)
LegendTexts = (“A”,“B”,“C”,“D”)
pyplot.title(“R (” + runname +")")
pyplot.xlabel(“Index”)
pyplot.ylabel(“Noise (dB)”)
pyplot.grid(True)
pyplot.hold(True)
pyplot.plot(np.transpose(ResidN),’.-’)
pyplot.legend(LegendTexts,loc=1)
pyplot.axis([0, 2*nIts, -33, -25])
pyplot.savefig(dirname + ‘\results.emf’,format=‘emf’)
pyplot.figure()
pyplot.hold(True)
pyplot.title(“Coefs”)
pyplot.xlabel(“Coef Index”)
pyplot.ylabel(“Coef Value”)
pyplot.grid(True)
pyplot.hold(True)
pyplot.plot(np.transpose(EchoA[0:nIts-1:echoPlotDec,:]),’.-’)
pyplot.plot(np.transpose(EchoA[nIts:2nIts-1:echoPlotDec,:]),’-’)
pyplot.axis([0, 160, -0.5, 2])
pyplot.savefig(dirname + ‘\CoefsA.emf’,format=‘emf’)
pyplot.figure()
pyplot.hold(True)
pyplot.title("MaxAbs, Old = ‘.’, New = ‘*’ ")
pyplot.xlabel(“Iteration”)
pyplot.ylabel(“o/p (LSBs)”)
pyplot.grid(True)
pyplot.hold(True)
pyplot.plot(np.transpose(MaxOld),’.-’)
pyplot.plot(np.transpose(MaxNew),’*-’)
pyplot.axis([0, 2*nIts, 32, 128])
pyplot.savefig(dirname + ‘\MaxAbsA.emf’,format=‘emf’)
pyplot.figure()
pyplot.hold(True)
pyplot.title(“MaxAbs”)
pyplot.xlabel(“Iteration”)
pyplot.ylabel("(LSBs)")
pyplot.grid(True)
pyplot.hold(True)
pyplot.plot(np.transpose(MaxSl),’.-’)
pyplot.axis([0, 2*nIts, 0, 64])
pyplot.savefig(dirname + ‘\MaxAbsSl.emf’,format=‘emf’)
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()