Memory footpring of python objects

B

BlueBird

Hi,

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.

cheers,

Philippe
 
C

Chris Rebert

Hi,

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.

sys.getsizeof() - http://docs.python.org/library/sys.html#sys.getsizeof

Cheers,
Chris
 
M

Marco Mariani

BlueBird said:
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.

Have you thought of using something like the flyweight pattern and a
compact data representation like a numpy array?
 
B

BlueBird

Only in Python 2.6.

But if you search Activestate, there's a recipe to do the same thing. Ah,
here it is:

http://code.activestate.com/recipes/546530/

Thanks. "There is always a tool that makes you realise how you are
less smart than you thought" - Andrew Morton, about git.

I had a bit of hard time getting through the different sizes reported
and the vocabulary. From what I understood, basicsize is the size of
the object alone, without the size of his references. And asizeof( x,
code=False, limit=100 ) will give me the memory footprint of a new
instance of an object.

In my case, I just want an object with named attributes.

My two attempts for comparison:
import sys, asizeof

def print_sizeof( v_name, v ):
print 'Size of %s: %d, %d' % (v_name,
asizeof.flatsize(v),
asizeof.asizeof(v, code=False, limit=100) )

def test_impl( v ):
assert v.a == 1
assert v.b == 'bbb'
assert v.c == False

### The reference, a dictionnary
ref_dict = {'a':1, 'b':'bbb', 'c':False }
print_sizeof( 'ref_dict', ref_dict )

### A modified dictionnary
class AttrDict (dict):
"""Dictionary allowing attribute access to its elements if they
are valid attribute names and not already existing methods."""

def __getattr__ (self, name):
return self[name]

attrDict = AttrDict( ref_dict )
test_impl( attrDict )
print_sizeof( 'attrDict', attrDict )

### Using __slots__
class ObjWithSlots(object):
__slots__ = [ 'a', 'b', 'c' ]

objWithSlots = ObjWithSlots()
for k in ref_dict:
setattr( ObjWithSlots, k, ref_dict[k] )
test_impl( objWithSlots )
print_sizeof( 'ObjectWithSlots', objWithSlots )

is giving me:
Size of ref_dict: 140, 304
Size of attrDict: 140,
304
Size of ObjectWithSlots: 36,
144

So, it looks like I could shrink down the structure to 144 bytes in a
simple case.

Marco, my objects have varying length data structures (strings) so I
don't think that would be possible. And it would clearly be overkill
in my case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top