Many not be the most intuitive and elegant solution (I'm just a Python newbie), but if your Python code is constrained to the usage of Python 2.2 language features, you could use Jython, and then (I'm hoping, since I've not tried this myself), use the Java Memory usage profiling/debugging tools.<br>
<br>Would be really interested to know the answer to the question, i.e. if there are any CPython solutions.<br><br><div class="gmail_quote">On Tue, May 6, 2008 at 6:57 PM, David <<a href="mailto:wizzardx@gmail.com">wizzardx@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi list.<br>
<br>
I've tried Googling for this and I checked the Python docs, but to no avail.<br>
<br>
What is the best way to debug memory usage in a Python script?<br>
<br>
I'd like to see what objects are using the most memory. Ideally this<br>
would be aware of references too. So if I have a small list that<br>
contains (references rather) some large variables, then I could see<br>
that the global list variable was responsible for X MB that couldn't<br>
be collected by the garbage collector.<br>
<br>
It would be nice if it could be used in a similar way to the 'du'<br>
Linux command. eg:<br>
<br>
eg output:<br>
<br>
>>> mem_debugger.pretty_print()<br>
A (type: list): 8 bytes, 10MB<br>
  - a (type: string): 6 MB, 0 bytes<br>
  - b (type: string): 4 MB, 0 bytes<br>
B (type: dict): 8 bytes, 5 MB<br>
  - d (type: string): 3 MB, 0 bytes<br>
  - c (type: string): 2 MB, 0 bytes<br>
<br>
In the output above, the 1st size is the memory used by the object<br>
itself, and the 2nd size is the memory used by objects it refers to. A<br>
& B are global vars (a string and a dict), a,b,c, and d are strings<br>
that were added to A & B at some point in the past, and aren't refered<br>
to by anything else. Also, the output is in descending order of size.<br>
<br>
Are there any tools/modules/etc I can use like this?<br>
<br>
David.<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>regards,<br>Banibrata<br><a href="http://www.linkedin.com/in/bdutta">http://www.linkedin.com/in/bdutta</a>