Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Possible Duplicate:
Python memory profiler

I've got a fairly complex (about 20,000) line Python program which after some development has started consuming increasing amounts of memory when it runs. What are the best tools and techniques for finding out what all the memory is being used for?

Usually this comes down to either unexpectedly keeping references to objects, or extension module bugs (which isn't particularly likely since we've been using the Python 2.4 installation for a while).

We are using various third party libraries such as Twisted, Twisted Conch and MySQLdb.

like image 353
Dickon Reed Avatar asked Feb 10 '09 13:02

Dickon Reed


People also ask

How do I detect a memory leak in Python?

The use of debugging method to solve memory leaks You'll have to debug memory usage in Python using the garbage collector inbuilt module. That will provide you a list of objects known by the garbage collectors. Debugging allows you to see where much of the Python storage memory is being applied.

How do I know if my code has a memory leak?

The simplest way to detect a memory leak is also the way you're most likely to find one: running out of memory. That's also the worst way to discover a leak! Before you run out of memory and crash your application, you're likely to notice your system slowing down.


1 Answers

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.

like image 56
S.Lott Avatar answered Oct 22 '22 02:10

S.Lott