473,555 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there any way to find out sizeof an object

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
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 1193
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

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.

Jun 27 '08 #2
On Jun 24, 6:00*am, srinivasan srinivas <sri_anna...@ya hoo.co.in>
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

* * * 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:



Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
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
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"; }
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
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;
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
by: Raghu | last post by:
Is there some thing like sizeof operator that would give me the size of the object? Thanks.
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..??
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...
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>
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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. ...
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...
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...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
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...
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.