By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,603 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,603 IT Pros & Developers. It's quick & easy.

Is there any way to find out sizeof an object

P: n/a
Hi,
I have written a class which has some attributes. I want to know how do i find out the size of an*instance of this class??
class T(object):
*** def __init__(self, fn_name, *args, **kwds):
*** *** self.fn_name = fn_name
*** *** self.args = args
*** *** self.kwds = kwds
Thanks,
Srini
Bollywood, fun, friendship, sports and more. You name it, we have it on http://in.promos.yahoo.com/groups/bestofyahoo/
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
I have written a class which has some attributes. I want to know how
do i find out the size of an instance of this class??
class T(object):
def __init__(self, fn_name, *args, **kwds):
self.fn_name = fn_name
self.args = args
self.kwds = kwds
In Python 2.6, you can use sys.getsizeof to find out how large an object
is. Notice that this is a really tricky question: Assuming you have

t = T(1,2,3, a="hello", b="world")

then you should certainly consider the size atleast

sys.getsizeof(t) + sys.getsizeof(t.__dict__)

But then you also have the attribute values (i.e. the tuple resp.
dict), which you might want to account for:

+ sys.getsizeof(t.args) + sys.getsizeof(t.dict)

Now, what to do about the sizes of the various items in the dict
and the tuple? One could do

total = ... # from above
for o in t.args:
total+=sys.getsizeof(o)
for k,v in t.kwds.items():
total+=sys.getsizeof(k)+sys.getsizeof(v)

However, that might be accounting too much, since the at least
the keys of the dict are shared across other dicts. Likewise,
the integers are shared, and the string literals (as written
above) would be shared if the very same T constructor is run
twice.

In short, "size of an instance" can't be answered without
a specific instance (i.e. you can't answer it in advance for
all instances), plus even for a single instance, its difficult
to answer.

Regards,
Martin
Jun 27 '08 #2

P: n/a
On Jun 24, 6:00*am, srinivasan srinivas <sri_anna...@yahoo.co.in>
wrote:
Hi,
I have written a class which has some attributes. I want to know how do i find out the size of an*instance of this class??
class T(object):
*** def __init__(self, fn_name, *args, **kwds):
*** *** self.fn_name = fn_name
*** *** self.args = args
*** *** self.kwds = kwds
Thanks,
Srini

* * * Bollywood, fun, friendship, sports and more. You name it, we have it onhttp://in.promos.yahoo.com/groups/bestofyahoo/
Check memory
Create a million
Check memory
Do maths!

;-)

- Paddy.
Jun 27 '08 #3

P: n/a
Here are a few little tools that I developed to do this kind of thing:

http://allmydata.org/trac/pyutil/bro...til/memutil.py

Regards,

Zooko
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.