How can I find memory leaks in my Python program? [duplicate] How can I find memory leaks in my Python program? [duplicate] python python

How can I find memory leaks in my Python program? [duplicate]


Generally, failing to close cursors is one of the most common kinds of memory leaks. The garbage collector can't see the MySQL resources involved in the cursor. MySQL doesn't know that the Python side was released unless the close() method is called explicitly.

Rule of thumb. Open, use and close cursors in as short a span of code as you can manage.


Python's memory is managed by a garbage collector. In general, there shouldn't be a problem with memory leaking (definitely not for Python2.5 and above), unless you happen to be writing extension modules in C/C++. In that case, Valgrind (Blog post -http://bruynooghe.blogspot.com/2008/12/finding-memory-leaks-in-python.html) might be helpful. I found that this person - http://mg.pov.lt/blog/hunting-python-memleaks has used PDB and matplotlib to trace a memory leak. I hope this helps, I have no experience fixing Python memory leaks.