[Tutor] Memory Leak?

Kent Johnson kent37 at tds.net
Wed May 7 22:28:20 CEST 2008


On Wed, May 7, 2008 at 3:50 PM, Keith Suda-Cederquist <kdsudac at yahoo.com> wrote:
> Hi,
>
> I'm doing some image processing using PIL and SciPy. Individual images are
> 2000x2000 pixels with each pixel being 16 bits, so a single image is around
> 7 MB in size.
>
> I've noticed that while my code is running the amount of memory being used
> (as reported by Windows Task Manager) by Python gradually increases.  It
> continues to increase if I re-run the same code again from within iPython
> (everytime I re-run the code the memory used increases by about 300 MB).  So
> after re-running the code several times (while debugging and/or developing)
> I will eventually get a memory exception (<type 'exceptions.MemoryError'>).

7 * 40 is close to 300 so I guess you are somehow not freeing the images.

> def function1(filename):
>     image1=im2array(filename)
>     #im2array is a function I wrote to open a tiff image as a scipy array
>     #average the 2000x2000 2d array into a single 2000x1 1d array
>     # perform some scaling on the 2000x1 1d array
>     #take the FFT of the 2000x1 1d array
>     result=[fftpeak, fftmaxamplitude]  #output is just two float values
>     return result
>
> filenames=['file1.tif','file2.tif',...]  #about 40 image filenames in this
> list

This looks ok, I guess any problem is in im2array.

> results=[]
> for ind in xrange(0,len(filenames):
>     results.append(function1(filenames[ind]))

This can be written much more simply with a list comprehension:
  results = [ function1(name) for name in filenames ]

Kent


More information about the Tutor mailing list