473,657 Members | 2,504 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 1199
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.gets izeof(o)
for k,v in t.kwds.items():
total+=sys.gets izeof(k)+sys.ge tsizeof(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...@ya hoo.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
3547
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 know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
4
3101
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 derived from it. At a particular point in my application I need the size of the object pointed to by the base pointer. Eg
2
2456
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
4267
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
2
1723
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 them out? in linux\mm\Slab.c, typedef struct slab_s { struct list_head list; unsigned long colouroff;
11
3598
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
3
1336
by: Raghu | last post by:
Is there some thing like sizeof operator that would give me the size of the object? Thanks.
21
13803
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 the size of integer on it. Is it Correct?? Or there any other way of finding it out? Do I need to mention that through a C++ program..??
43
4912
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. That is, replace calls to strcpy with say, my_strcpy(dest,src) which will internally find the destination buffer length. For this we need to know the destination buffer size. For statically allocated strings sizeof is returning the length of the...
9
1921
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 like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.