I wrote a simple tool that could take Web logs and replay them against a server in "real time". I was performance testing my Web app over the course of a day by hitting it with many days worth of Web logs at the same time.
By monitoring top, I found out that it was leaking memory. I was excited to try out Guppy, but it didn't help. Neither did playing around with the gc module. I had too many objects coming and going to make sense of it all.
Hence, I fell back to a simple process of elimination. Divide-and-conquer! I would make a change to the code, then I would exercise the code in a loop and monitor the output from top for ever-increasing memory usage.
Several hours later, I was able to nail it down to this simple repro:
By monitoring top, I found out that it was leaking memory. I was excited to try out Guppy, but it didn't help. Neither did playing around with the gc module. I had too many objects coming and going to make sense of it all.
Hence, I fell back to a simple process of elimination. Divide-and-conquer! I would make a change to the code, then I would exercise the code in a loop and monitor the output from top for ever-increasing memory usage.
Several hours later, I was able to nail it down to this simple repro:
# This program leaks memory rather quickly. Removing the charsetIt makes sense that if the memory leak is at the C level, I might not be able to find it with Python-level tools. I'll go hunting tomorrow to see if the MySQLdb team has already fixed it, and if not, I'll submit a bug.
# parameter fixes it.
import MySQLdb
import sys
while True:
connection = MySQLdb.connect(user='user', passwd='password',
host='localhost', db='development',
charset='utf8')
try:
cursor = connection.cursor()
cursor.execute('select * from mytable where false')
sys.stdout.write('.')
sys.stdout.flush()
finally:
connection.close()
Comments
When I was debugging Chandler, I also tried to use Heapy, PySizer, and a nice frontend I built just for Chandler which took information from gc etc. I also found a commercial tool called Python Memory Validator which actually helped me find one bug in Chandler. I found one bug in the Python Memory Validator and the company was quick to fix it. They provide free evaluation if you want to try it out.
And just now as I was doing some searches I found muppy. Don't know anything more about it.
Still the bottom line is that none of the tools I have used were easy to use. There is definitely room for a good, simple memory debugger for Python.
Ah, if only MySQL had a clean, well documented interface, eh? It'd be great to have a pure-Python, asynchronous driver.
You can call cursor.fetchall(), but you shouldn't need to.
http://sourceforge.net/tracker/index.php?func=detail&aid=2125250&group_id=22307&atid=374932
apt-get install libmysqlclient15-dev libmysql++-dev python-dev:
+zlib1g-dev
+libmysql++2c2a
+python2.5-dev
cd
svn co https://mysql-python.svn.sourceforge.net/svnroot/mysql-python/trunk mysql-python
cd mysql-python/MySQLdb
python setup.py build
python setup.py bdist_egg
sudo easy_install --always-unzip dist/MySQL_python-1.3.0-py2.5-*.egg
cd ../..
rm -rf mysql-python/
Thanks!
I enjoyed reading your blog.
==
Mike, hello! I prefer SQLAlchemy when I have a little bit of really complex data, but I prefer straight SQL when I have a ton of really simple data. Cheers!
==
anonymous, thanks for reading!