Clearing figures from memory

I am having problems clearing figures from memory. After saving the
figure, I use pyplot.close() on the figure handle and then del all of
the data and figure, as shown here:

fig.savefig('plots/%(record_id)05i' % recording)
plt.close(fig)

del accel, fourier_amp, fig, time, disp
gc.collect()

Despite this, the figures don't appear to be closing. I am trying to
make 30k plots and I have to kill script every couple thousand and
restart because I run out of memory.

Any suggestions?

Albert

I am having problems clearing figures from memory. After saving the
figure, I use pyplot.close() on the figure handle and then del all of
the data and figure, as shown here:

fig.savefig('plots/%(record_id)05i' % recording)
plt.close(fig)

del accel, fourier_amp, fig, time, disp
gc.collect()

Despite this, the figures don't appear to be closing. I am trying to
make 30k plots and I have to kill script every couple thousand and
restart because I run out of memory.

Any suggestions?

You are running a standalone script, correct? Make sure you are using only the agg backend. Before the first import of pyplot, do

import matplotlib
matplotlib.use("agg")

I don't know if that will help, but it can't hurt.

You might find matplotlib.cbook.report_memory() to be useful in tracking down the problem.

Eric

···

On 03/23/2012 08:05 AM, Albert Kottke wrote:

Albert

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

A while ago I had exactly the same problem. I was running a script that create a lot of figures and saved them (as .png and .eps files) for viewing later (so not using the interactive viewing feature from pyplot). If I remember well, there was also a problem with the close() statement leaving some stuff in memory. Unfortunately I don't remember how the details went and how it got fixed. I guess I should have used the mailing list or created a bug report.

Anyway, when I need to generate a massive amount of figures these days I bypass the pyplot interface completely and go straight for the matplotlib API (at least as far as I understand this approach myself). When using pyplot, some figure tweaks did not persist upon saving, but that might as well have been due to my limited matplotlib knowledge. The procedure was inspired by this blog post: http://sjohannes.wordpress.com/2010/06/11/using-matplotlib-in-a-web-application/ and goes as follows:

from matplotlib.figure import Figure
# dependent on the backend you use
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigCanvas
# set figure size, dpi and initiate a workable fig object
fig = Figure(figsize=(figsize_x, figsize_y), dpi=dpi)
canvas = FigCanvas(fig)
fig.set_canvas(canvas)
# tweak white spacings
fig.subplots_adjust(left=wsleft, bottom=wsbottom, right=wsright,
top=wstop, wspace=wspace, hspace=hspace)
# big main title
fig.suptitle(grandtitle, size='x-large')
# create a subplot
ax = fig.add_subplot(nr_rows, nr_cols, plot_nr)

# do all your plotting stuff here on ax

# save the figure and close
fig.savefig('/path/to/figure/figname.png')
canvas.close()
fig.clear()

If interested, I can give you a more elaborate and working example of which parameters I tune how exactly.

Regards,
David

···

On 23/03/12 19:45, Eric Firing wrote:

On 03/23/2012 08:05 AM, Albert Kottke wrote:

I am having problems clearing figures from memory. After saving the
figure, I use pyplot.close() on the figure handle and then del all of
the data and figure, as shown here:

fig.savefig('plots/%(record_id)05i' % recording)
plt.close(fig)

del accel, fourier_amp, fig, time, disp
gc.collect()

Despite this, the figures don't appear to be closing. I am trying to
make 30k plots and I have to kill script every couple thousand and
restart because I run out of memory.

Any suggestions?

You are running a standalone script, correct? Make sure you are using
only the agg backend. Before the first import of pyplot, do

import matplotlib
matplotlib.use("agg")

I don't know if that will help, but it can't hurt.

You might find matplotlib.cbook.report_memory() to be useful in tracking
down the problem.

Eric

Albert

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users