memory leak problem with arrays

Carl Banks invalidemail at aerojockey.com
Thu Jun 15 08:32:07 EDT 2006


sonjaa wrote:
> I've created a cellular automata program in python with the numpy array
> extensions. After each cycle/iteration the memory used to examine and
> change the array as determined by the transition rules is never freed.

Are you aware that slicing shares memory?  For example, say you defined
a grid to do the automata calculations on, like this:

grid = numpy.zeros([1000,1000])

And then, after running it, you took a tiny slice as a region of
interest, for example:

roi = grid[10:20,10:20]

Then deleted grid:

del grid

Then stored roi somewhere, for example:

run_results.append(roi)

If you do this, the memory for the original grid won't get freed.
Although grid was deleted, roi still contains a reference to the whole
1000x1000 array, even though it's only a tiny slice of it.  Your poorly
worded description--no offense--of what you did suggests that this is a
possibility in your case.  I recommend you try to create a new array
out of any slices you make, like this (but ONLY if the slice doesn't
depend on the memory being shared):

roi = numpy.array(grid[10:20,10:20])

This time, when you del grid, there is no object left referencing the
array data, so it'll be freed.

This might not be your problem.  Details are important when asking
questions, and so far you've only given us enough to speculate with.

Carl Banks




More information about the Python-list mailing list