Home All Groups Group Topic Archive Search About
Author
9 Feb 2007 7:28 PM
ThunderMusic
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

Author
9 Feb 2007 7:42 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,


"ThunderMusic" <NoSpAmdanlatathotmaildotcom@NoSpAm.com> wrote in message
news:%23Yf8bBITHHA.4832@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
Author
10 Feb 2007 6:24 PM
Göran_Andersson
ThunderMusic wrote:
Show quote
> 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

AddThis Social Bookmark Button