John Hunter | 2 Nov 04:41 2005

Re: Memory leak with pcolor

>>>>> "Alexander" == Alexander Mont <alexmont1@...> writes:

    Alexander> Thanks for your advice with installing matplotlib on
    Alexander> cygwin. I downloaded and installed the windows binaries
    Alexander> and it worked.  Anyway, the reason that I didn't want
    Alexander> to use binaries in the first place was because I wanted
    Alexander> to modify the matplotilb source code. But it seems like
    Alexander> even with the binaries, if I change the source code
    Alexander> then it will still affect the operation of the program
    Alexander> when I run it, which is what I want.

    Alexander> In particular, I am looking to speed up the pcolor()
    Alexander> function because it runs exceedingly slow with large
    Alexander> mesh sizes. I believe the reason it is running slow is
    Alexander> because of a memory leak. When I do the following:

    Alexander> from pylab import * n=200
    Alexander> [x,y]=meshgrid(arange(n+1)*1./n,arange(n+1)*1./n)
    Alexander> z=sin(x**2 + y**2)

    Alexander> and then do

    Alexander> pcolor(x,y,z)

    Alexander> repeatedly, the memory usage increases by about 15 MB
    Alexander> each time, and it runs progressively slower.each

At least with matplotlib CVS (and I don't think it's a CVS vs 0.84
issue) the memory consumption is rock solid with your example (see
below for my test script).  What is your default "hold" setting in rc?
If True, you will be overlaying plots and will get the behavior you
describe.  In the example below, I make sure to "close" the figure
each time -- a plain clear with clf should suffice though.  My guess
is that you are repeatedly calling pcolor with hold : True and are
simply overlaying umpteen pcolors (to test for this, print the length
of the collections list

  ax = gca()
  print len(ax.collections)

if this length is growing, you've found your problem.  A simple


should suffice.

You can also change the default hold setting in your config file


Example code:

#!/usr/bin/env python

import os, sys, time
import matplotlib
from pylab import *

def report_memory(i):
    pid = os.getpid()
    a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines()
    print i, '  ', a2[1],
    return int(a2[1].split()[1])

# take a memory snapshot on indStart and compare it with indEnd

indStart, indEnd = 30, 201
for i in range(indEnd):

    figure(1); clf()

    z=sin(x**2 + y**2)
    savefig('tmp%d' % i, dpi = 75)

    val = report_memory(i)
    if i==indStart: start = val # wait a few cycles for memory usage to stabilize

end = val
print 'Average memory consumed per loop: %1.4fk bytes\n' % ((end-start)/float(indEnd-indStart))

Average memory consumed per loop: 0.0053k bytes

SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: