memory usage (how to debug a memory leak?)

Mark Hammond mhammond at skippinet.com.au
Mon Mar 10 22:28:49 CET 2003


Marcus Stojek wrote:
> Hi,
> 
> I have written a middle scale app using Python 2.2.2, wxPython 2.3.
> and vtk 4.0 (graphics) on Windows NT.  I have some c-extensions too
> (swig 3.1.17)
> 
> The app has one ParentFrame and I can open and close ChildFrames
> (MDI) and visualilze things in them using vtk.
> 
> Everything works as it should, but  the task manager in Windows shows
> that the memory usage is not reduced when I close one or more of the
> Child Windows . If I close the app all memory is freed and I never get
> an error message of any kind. 
> 
> I tried to del() everything before closing a window, I checked all
> refcounts and I checked the swig interfaces. No memory is allocated
> inside my c-extensions. Instead I pass a Numeric.array of the right
> size to the c-extension and inside the c-code this array is filled
> with result values.
> 
> Now my question is not what is wrong with my code, but how I can find
> out.
> 
> Is there any way to get a list like [(object1,sizeof(object1)),...]?
> Or a Python tool that tells my something about memory usage?
> How do you hunt a memory leak?
> Or is it just the Task Manager that's fooling me?

A good starting point is sys.getrefcount(), and in debug builds, 
sys.gettotalrefcount().  These can show you if you are leaking 
references which is a very common leak in Python extensions.

Once you identify you have one, you basically have to narrow it down via 
Python code to the one or 2 extension functions with the problem.

Mark.





More information about the Python-list mailing list