Felix
Felix

Reputation: 739

Release matplotlib memory in Spyder IDE

If I run the following code

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import scipy.misc
import gc

def plot(i):
  lena = scipy.misc.lena()
  fig = plt.figure(frameon=False,figsize=(2.,2.),dpi=256)
  ax = fig.add_axes([0, 0, 1, 1])
  ax.axis('off')
  ax.imshow(lena, cmap=plt.cm.gray)
  filename = '/tmp/test_{:03d}.png'.format(i)
  with open(filename, 'w') as outfile:
    fig.canvas.print_png(outfile)
  plt.close(fig)
  del fig
  gc.collect()

for i in range(100):
  plot(i)
  print(i)

in Spyder (version 2.3.5.2 on gentoo linux with python 2.7.10, IPython 3.2.1), I see a linear increase in memory with the iteration number i until eventually I run out of memory. By "run in Spyder" I mean marking the text and pressing F9. However, if I run it in a detached IPython terminal, the memory consumption remains constant.

Is there a way to force the release of memory in Spyder after potting with matplotlib, in particular when the output went to a file and not to the Spyder-internal IPython terminal?

Upvotes: 2

Views: 749

Answers (1)

Siva
Siva

Reputation: 71

I came across similar problem while plotting 1000 plots. Before closing the plot, I used the command plt.show(). Then I used the plt.close() and garbage collect etc... This significantly minimized the memory leakage. Though I don't know the exact reason, I found the plt.show() to handle the memory problem for me.

Upvotes: 1

Related Questions