March 26th, 2004, 06:12 AM
I have developed a rather large program in python that leaks memory somewhere. However I have no idea where. Does anyone know of tools that can help with finding memory leaks in python?
Alternatively is there some way to query the amount of memory a program uses from within python?
Thanks in advance,
March 26th, 2004, 09:56 AM
March 26th, 2004, 10:18 AM
If you are using a modern version of Python that has been compiled with garbage collection enabled, then you can use the 'gc' module in the standard library to get information about the objects known to the garbage collector, force garbage collection, trace object creation and deletion, etc. RTFM for full information.
For earlier versions of Python there is a tool called Cyclops written by Python Guru Tim Peters. This tracks down and reports cyclic object references. It is rather dated (1999) but should still work with current versions of Python. You can find it on the Python.org FTP server - http://www.python.org/ftp/python/con...c-1999/System/. There may be more recent versions around, but I have not found any. If garbage collection is enabled then cycles should not be such a problem unless an object cannot be freed by the GC.
There is also a function in the sys module 'getrefcount' that you can use to find out how many reference there are to an object.
Between these three, you should be able to track down any leaks in your Python code. Leaks in extension modules is another matter.
Hope this helps,
Dave - The Developers' Coach
Last edited by DevCoach; March 26th, 2004 at 10:42 AM.