how do I find memory leaks?
Neel Krishnaswami
neelk at brick.cswv.com
Wed Jul 21 19:26:25 EDT 1999
In article <7n4skv$8do at thalamus.wustl.edu>,
Heather A. Drury <heather at thalamus.wustl.edu> wrote:
>
>The program has gotten to a fairly advanced stage but I've run
>into what appears to be a major memory leak. I come
>from the C programming world and have previously used
>Purify to debug memory leaks. Alas, I thought using python/vtk
>would take care of all this memory garbage for me (although
>I'm probably not doing something I should be).
>
>Over time as the user continues to use the application, the
>computer starts using up swap space and eventually the program
>crashes. I haven't the faintest idea how to begin to debug this
>problem. I'm sure I need to provide some more detail but the
>code is about 2000 lines now and I'm not sure what detail
>would be useful.
>
>Any hints/suggestions/pointers would be greatly appreciated.
This sounds like a problem an awful lot like I had with NumPy
a few weeks ago. Basically, slices of arrays count as references
to it, which leads to some surprising memory wastage if you
don't know what's happening.
For example:
import Numeric
def waste_memory():
x = Numeric.zeros([1000,1000])
return x[0,0:2]
Each call to waste_memory() will generate a million-element array,
and return an array of two elements. However, the slice that's
returned will cause the *whole* 1000x1000 array to be kept in
memory.
You can fix this by wrapping the return in another call to
Numeric.array(), which creates a new array object and lets
the old one go out of scope properly.
E.g.:
def no_waste():
x = Numeric.zeros([1000,1000])
return Numeric.array( x[0,0:2] )
Hope this helps.
Neel
More information about the Python-list
mailing list