Memory leak somewhere?

I was trying to use matplotlib to plot a series of 2D images, but
python was using up a large amount of RAM very quickly. I don't know
matplotlib that well, so the chance are I am missing something, would
appreciate it if anyone can point me to the right direction.

I am using:
Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2

Example code to run in interpreter mode:

···

########################################
from numpy import zeros

x = 1651
y = 452
page = zeros((x, y)).astype('float')

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

for i in range(1000):
  fig = plt.figure()
  ax = fig.add_subplot(111)
  cax = ax.imshow(page, cmap=plt.cm.spectral_r, extent=(-44, 176, -30,
30), interpolation = 'bicubic', vmin = -0.003, vmax = 0.003)
  title = "Time = %(i)0.3es)" % {'i':i}
  ax.set_title(title,fontsize=14)
  
  fig.colorbar(cax, ticks=[-2e-3, -1e-3, 0, 1e-3, 2e-3],
orientation='horizontal')
  
  fig.savefig('_tmp.' + str(i) + ".png", dpi=300)
  
############### EOF ################

I tired to delete everything in the namespace, but the only way I can
release the ram is by killing the python session.

Thanks for all the helps in advance.

iCy

Does it help if you add a call to "plt.clf()" to the bottom of the loop?

The pyplot interface keeps a reference around to every figure created until they are destroyed so that it can be obtained again by number (this is functionality inspired by matlab). Alternatively, you can use the object-oriented interface to create the figure, which does not have this behavior, e.g., replace

   fig = plt.figure()

with

   from matplotlib import figure
   fig = figure.Figure()

If all this doesn't help, let me know and I'll look further.

Cheers,
Mike

iCy-fLaME wrote:

···

I was trying to use matplotlib to plot a series of 2D images, but
python was using up a large amount of RAM very quickly. I don't know
matplotlib that well, so the chance are I am missing something, would
appreciate it if anyone can point me to the right direction.

I am using:
Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2

Example code to run in interpreter mode:

########################################
from numpy import zeros

x = 1651
y = 452
page = zeros((x, y)).astype('float')

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

for i in range(1000):
  fig = plt.figure()
  ax = fig.add_subplot(111)
  cax = ax.imshow(page, cmap=plt.cm.spectral_r, extent=(-44, 176, -30,
30), interpolation = 'bicubic', vmin = -0.003, vmax = 0.003)
  title = "Time = %(i)0.3es)" % {'i':i}
  ax.set_title(title,fontsize=14)
  
  fig.colorbar(cax, ticks=[-2e-3, -1e-3, 0, 1e-3, 2e-3],
orientation='horizontal')
  
  fig.savefig('_tmp.' + str(i) + ".png", dpi=300)
  
############### EOF ################

I tired to delete everything in the namespace, but the only way I can
release the ram is by killing the python session.

Thanks for all the helps in advance.

iCy

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://www.creativitycat.com _______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
  
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

This code creates 1000 different figures -- either reuse the same
figure and clear it as Michael suggests

  fig = plt.figure(1) # by putting 1 here you reuse the same fig
  fig.clf() # and clear it

or close the figure in the loop

  fig = plt.figure()
  # draw and save here
  plt.close(fig)

JDH

···

On Tue, May 26, 2009 at 7:39 AM, Michael Droettboom <mdroe@...86...> wrote:

Does it help if you add a call to "plt.clf()" to the bottom of the loop?

The pyplot interface keeps a reference around to every figure created
until they are destroyed so that it can be obtained again by number
(this is functionality inspired by matlab). Alternatively, you can use
the object-oriented interface to create the figure, which does not have
this behavior, e.g., replace

  fig = plt.figure()

with

  from matplotlib import figure
  fig = figure.Figure()

If all this doesn't help, let me know and I'll look further.

Cheers,
Mike

iCy-fLaME wrote:

I was trying to use matplotlib to plot a series of 2D images, but
python was using up a large amount of RAM very quickly. I don't know
matplotlib that well, so the chance are I am missing something, would
appreciate it if anyone can point me to the right direction.

I am using:
Python 2.4.3 (#1, Jan 21 2009, 01:11:33)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2

Example code to run in interpreter mode:

########################################
from numpy import zeros

x = 1651
y = 452
page = zeros((x, y)).astype('float')

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

for i in range(1000):
      fig = plt.figure()
      ax = fig.add_subplot(111)
      cax = ax.imshow(page, cmap=plt.cm.spectral_r, extent=(-44, 176, -30,
30), interpolation = 'bicubic', vmin = -0.003, vmax = 0.003)
      title = "Time = %(i)0.3es)" % {'i':i}
      ax.set_title(title,fontsize=14)

      fig.colorbar(cax, ticks=[-2e-3, -1e-3, 0, 1e-3, 2e-3],
orientation='horizontal')

      fig.savefig('_tmp.' + str(i) + ".png", dpi=300)

You made my day!

Long life to The “close()”

All my ram and swap file was sucked every time a run my script to generate 260 png images…almost killing my ubuntu!

···

On Tue, May 26, 2009 at 2:23 PM, John Hunter <jdh2358@…287…> wrote:

On Tue, May 26, 2009 at 7:39 AM, Michael Droettboom <mdroe@…86…> wrote:

Does it help if you add a call to “plt.clf()” to the bottom of the loop?

The pyplot interface keeps a reference around to every figure created

until they are destroyed so that it can be obtained again by number

(this is functionality inspired by matlab). Alternatively, you can use

the object-oriented interface to create the figure, which does not have

this behavior, e.g., replace

fig = plt.figure()

with

from matplotlib import figure

fig = figure.Figure()

If all this doesn’t help, let me know and I’ll look further.

Cheers,

Mike

iCy-fLaME wrote:

I was trying to use matplotlib to plot a series of 2D images, but

python was using up a large amount of RAM very quickly. I don’t know

matplotlib that well, so the chance are I am missing something, would

appreciate it if anyone can point me to the right direction.

I am using:

Python 2.4.3 (#1, Jan 21 2009, 01:11:33)

[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2

Example code to run in interpreter mode:

########################################

from numpy import zeros

x = 1651

y = 452

page = zeros((x, y)).astype(‘float’)

import matplotlib

matplotlib.use(‘Agg’)

import matplotlib.pyplot as plt

for i in range(1000):

  fig = plt.figure()
  ax = fig.add_subplot(111)
  cax = ax.imshow(page, cmap=plt.cm.spectral_r, extent=(-44, 176, -30,

30), interpolation = ‘bicubic’, vmin = -0.003, vmax = 0.003)

  title = "Time = %(i)0.3es)" % {'i':i}
  ax.set_title(title,fontsize=14)
  fig.colorbar(cax, ticks=[-2e-3, -1e-3, 0, 1e-3, 2e-3],

orientation=‘horizontal’)

  fig.savefig('_tmp.' + str(i) + ".png", dpi=300)

This code creates 1000 different figures – either reuse the same

figure and clear it as Michael suggests

fig = plt.figure(1) # by putting 1 here you reuse the same fig

fig.clf() # and clear it

or close the figure in the loop

fig = plt.figure()

draw and save here

plt.close(fig)

JDH


Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT

is a gathering of tech-side developers & brand creativity professionals. Meet

the minds behind Google Creative Lab, Visual Complexity, Processing, &

iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian

Group, R/GA, & Big Spaceship. http://www.creativitycat.com


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users