How do you debug memory usage?

David wizzardx at gmail.com
Tue May 6 09:27:54 EDT 2008


Hi list.

I've tried Googling for this and I checked the Python docs, but to no avail.

What is the best way to debug memory usage in a Python script?

I'd like to see what objects are using the most memory. Ideally this
would be aware of references too. So if I have a small list that
contains (references rather) some large variables, then I could see
that the global list variable was responsible for X MB that couldn't
be collected by the garbage collector.

It would be nice if it could be used in a similar way to the 'du'
Linux command. eg:

eg output:

>>> mem_debugger.pretty_print()
A (type: list): 8 bytes, 10MB
  - a (type: string): 6 MB, 0 bytes
  - b (type: string): 4 MB, 0 bytes
B (type: dict): 8 bytes, 5 MB
  - d (type: string): 3 MB, 0 bytes
  - c (type: string): 2 MB, 0 bytes

In the output above, the 1st size is the memory used by the object
itself, and the 2nd size is the memory used by objects it refers to. A
& B are global vars (a string and a dict), a,b,c, and d are strings
that were added to A & B at some point in the past, and aren't refered
to by anything else. Also, the output is in descending order of size.

Are there any tools/modules/etc I can use like this?

David.



More information about the Python-list mailing list