Does anyone know if there's a way to see how much memory a particular
object is using in .NET?
For example, suppose I have a HashTable or NameValueCollection and I
keep adding things to it. I want to know the total number of bytes the
object is using.
Is there any way to do this?
Thanks in advance!
Morten Wennevik - 16 Dec 2004 21:51 GMT
Hi Jon Hyland,
The only way I can think of is using Marshal.SizeOf(object).

Signature
Happy Coding!
Morten Wennevik [C# MVP]
Imran Koradia - 16 Dec 2004 22:12 GMT
Just a point to note - Marshal.SizeOf will return the unmanaged size of the
object. The runtime adds overhead for each type so the managed size would be
slightly different.
That said, here's an excellent article in the december issue of the MSDN
magazine on memory usage and optimazations:
http://msdn.microsoft.com/msdnmag/issues/05/01/MemoryOptimization/default.aspx
According to the article, you could use performance counters and measure the
change in the heap memory when creating your objects to get a feel of the
size your objects are occupying. Also, you could use the CLR profiler (link
in the article for the download) to monitor memory usage.
hope that helps..
Imran.
> Does anyone know if there's a way to see how much memory a particular
> object is using in .NET?
[quoted text clipped - 6 lines]
>
> Thanks in advance!
Mr. Mountain - 17 Dec 2004 01:11 GMT
I have had excellent results using .NET Memory Profiler. I believe they have
a free trial.
> Does anyone know if there's a way to see how much memory a particular
> object is using in .NET?
[quoted text clipped - 6 lines]
>
> Thanks in advance!