Memory footpring of python objects

Nick Craig-Wood nick at craig-wood.com
Thu Apr 23 07:30:05 EDT 2009


BlueBird <phil at freehackers.org> wrote:
>  I have a program that manages several thousands instances of one
>  object. To reduce memory
>  consumption, I want of course that specific object to have the
>  smallest memory footpring possible.
> 
>  I have a few ideas that I want to experiment with, like using
>  __slots__, using a tuple or using a dict. My
>  question is: how do I know the memory footprint of a given python
>  object ? I could not find any
>  builtin functions for this in the documentation.

I managed to 1/3 the memory requirement of our program by using
__slots__ on the most common class (several hundred thousand
instances!).

When doing these optimisations I ran a repeatable script and measured
the total memory usage using the OS tools (top in my case).

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list