See memory allocated? HashTable?

  • Thread starter Thread starter jonhyland
  • Start date Start date
J

jonhyland

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!
 
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.
 
I have had excellent results using .NET Memory Profiler. I believe they have
a free trial.
 
Back
Top