python loop increases its memory usage rapidly -
i have python script runs loop. within loop, function dodebuginfo
called, once per loop iteration. function prints pictures hard disk using matplotlib, export kml file , other calculations, , returns nothing.
i'm having problem python, each run, function dodebuginfo
eats more , more ram. guess variable increasing it's size on each loop.
i added following lines before , after call:
print '=== before: ' + str(resource.getrusage(resource.rusage_self).ru_maxrss / 1000) dodebuginfo(inputs) print '=== after: ' + str(resource.getrusage(resource.rusage_self).ru_maxrss / 1000)
the output is:
=== before: 71598.08 === after: 170237.952 === before: 170237.952 === after: 255696.896 === before: 255696.896 === after: 341409.792
as can see, before call program has memory footprint, , after call increases, stays stable until before next call.
why this? since dodebuginfo(inputs)
function returns nothing, how can variables stay on memory? there need clear variables @ end of function?
edit: dodebuginfo
imports functions:
def plot_line(x,y,kind,lab_x,lab_y,filename): fig = plt.figure(figsize=(11,6),dpi=300) ax = fig.add_subplot(111) ax.grid(true,which='both') #print 'plotting' if type(x[0]) datetime.datetime: #print 'datetime detected' ax.plot_date(matplotlib.dates.date2num(x),y,kind) ax.fmt_xdata = dateformatter('%h') ax.autoscale_view() fig.autofmt_xdate() else: #print 'no datetime' ax.plot(x,y,kind) xlabel = ax.set_xlabel(lab_x) ax.set_ylabel(lab_y) fig.savefig(filename,bbox_extra_artists=[xlabel], bbox_inches='tight') def plot_hist(x,nbins,lab_x,lab_y,filename): fig = plt.figure(figsize=(11,6),dpi=300) ax = fig.add_subplot(111) ax.grid(true,which='both') ax.hist(x,nbins) xlabel = ax.set_xlabel(lab_x) ax.set_ylabel(lab_y) fig.savefig(filename,bbox_extra_artists=[xlabel], bbox_inches='tight')
and plots 10 figures disk using like:
plot_line(index,alt,'-','drive index','altitude in m',output_dir + 'name.png')
if comment lines use plot_line
problem not happen, leak should on lines of code.
thanks
the problem relies on many figures being created , never closed. somehow python keeps them alive.
i added line
plt.close()
to each of plot functions plot_line
, plot_hist
, problem gone.
Comments
Post a Comment