473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Size of an object

Hi, is there an easy way to know how much memory an instance of an object
takes?

I mean, there is the sizeof(type), but this is not what I want... the
sizeof(type) returns the size of all the members of the class, but if the
class contains reference types like a collection or a string, it can't
tell...

What I need to know is, if there is a collection of strings, with each
strings taking 100kb and having 5 strings in the collection, I want to have
500kb (or approx) as a return... is there an easy, thread-safe way?

Thanks

ThunderMusic

btw, I'm using c# with framework 2.0
Feb 9 '07 #1
2 2988
Hi,
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
| Hi, is there an easy way to know how much memory an instance of an object
| takes?
|
| I mean, there is the sizeof(type), but this is not what I want... the
| sizeof(type) returns the size of all the members of the class, but if the
| class contains reference types like a collection or a string, it can't
| tell...

If you look into the archives you will see this is a recurrng question with
no clear answer.

No, you have no way to know how "big" an object is if you take into account
the references it can hold (or the references those referenced instnaces can
hold)
--
Ignacio Machin
machin AT laceupsolutions com
Feb 9 '07 #2
ThunderMusic wrote:
Hi, is there an easy way to know how much memory an instance of an object
takes?

I mean, there is the sizeof(type), but this is not what I want... the
sizeof(type) returns the size of all the members of the class, but if the
class contains reference types like a collection or a string, it can't
tell...

What I need to know is, if there is a collection of strings, with each
strings taking 100kb and having 5 strings in the collection, I want to have
500kb (or approx) as a return... is there an easy, thread-safe way?

Thanks

ThunderMusic

btw, I'm using c# with framework 2.0

You can add the size of the members in the object to get an approximate
value of how much memory it uses.

There is no way to get a definite size, though. Take a simple examle:

string a = new String('*', 5000);
string b = a;

If you look at each variable, it uses around 10000 bytes, but if you
look at them together, they don't use 20000 bytes in total. So, which of
them is it that is using the memory?

--
Göran Andersson
_____
http://www.guffa.com
Feb 10 '07 #3

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

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
6
by: Jon Jagger | last post by:
I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following....
6
by: cameron | last post by:
I need to get the size of an objet in memory. I have tried: System.IO.MemoryStream m = new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
5
by: Phil Jones | last post by:
Is there a way to determine the size (number of bytes) of an object? I figure this can be done by serializing the object to disk and measuring the file size - but I definately don't want to do...
8
by: Dave | last post by:
I am serialising an object to a memory mapped file (using the CreateFileMapping and MapViewOfFile p/invoke calls). These need to know the maximum size of the "file". I can put in a "good guess" ie...
6
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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...
0
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,...
0
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...

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.