How do I evaluate, in c#, the amount of memory taken up by any given object
?

Re: Memory taken by an object by Jan

Jan
Fri Sep 16 04:13:53 CDT 2005

Hi JezB,

Binary Serialize the object to disk.

Hope that helps,
Jan


Re: Memory taken by an object by Tasos

Tasos
Fri Sep 16 04:23:32 CDT 2005

int size = System.Runtime.InterpoServices.Marshal.SizeOf(object);


Re: Memory taken by an object by Miha

Miha
Fri Sep 16 04:33:41 CDT 2005

Hi Jan,

Just a note that this way you'll get an estimate and not an exact amount
(due to memory alignment and other context not serialized).
Perhaps the best way is to check it with a memory profiler..

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Jan Bannister (jancsharp.blogspot.com)" <jan.bannister@gmail.com> wrote in
message news:1126862033.319409.260360@g44g2000cwa.googlegroups.com...
> Hi JezB,
>
> Binary Serialize the object to disk.
>
> Hope that helps,
> Jan
>



Re: Memory taken by an object by Jon

Jon
Fri Sep 16 04:41:17 CDT 2005

> How do I evaluate, in c#, the amount of memory taken up by any
> given object?

Neither binary serialization nor marshalling will actually tell you
that information, and there's no way to determine it accurately, in a
guaranteed way.

However, each object has an 8 byte "overhead" plus the space taken for
its member fieds. Each reference is 4 bytes (on a 32 bit CLI), each
Int32 takes 4 bytes, etc. Where it becomes tricky is if you have
byte/short members - depending on attributes, these members may be
"packed" or not.

Don't forget though that two objects could both refer to the same third
object - you'd have to make sure you don't count that third object
twice if you're trying to sum the object sizes...

Jon


Re: Memory taken by an object by Jan

Jan
Fri Sep 16 05:01:41 CDT 2005

Thanks Miha,

Thats handy to know.

Jan


Re: Memory taken by an object by Miha

Miha
Fri Sep 16 05:54:30 CDT 2005

You're welcome :-D

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Jan Bannister (jancsharp.blogspot.com)" <jan.bannister@gmail.com> wrote in
message news:1126864195.435282.142250@g49g2000cwa.googlegroups.com...
> Thanks Miha,
>
> Thats handy to know.
>
> Jan
>