473,503 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there any way to find out sizeof an object

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
3 1182
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3535
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
4
3093
by: Gopal-M | last post by:
I have the problem with sizeof operator I also want to implement a function that can return size of an object. My problem is as follows.. I have a Base class, say Base and there are many class...
2
2449
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
2
4256
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
2
1714
by: Xiangliang Meng | last post by:
Hi, all. As far as I know, the speical arithmetic operator on void pointer is an externsion in GNU CC. However, I could not find the relative topics in C99. Would someone like to help me find...
11
3580
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
3
1327
by: Raghu | last post by:
Is there some thing like sizeof operator that would give me the size of the object? Thanks.
21
13759
by: codergem | last post by:
One common answer is that all compilers keep the size of integer the same as the size of the register on a particular architecture. Thus, to know whether the machine is 32 bit or 64 bit, just see...
43
4878
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
9
1909
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.