tags:

views:

399

answers:

5

Hello,

How do I find out about the memory used by each object in a program?

For example : I want to know how much memory(in Kb) is used by this object "someclassinstance"..

someclass someclassinstance=new someclass();

I can see the total memory used by the application in the task manager...But Is there a way to see detailed report about memory usage for each object instance?

Note:I've tried CLR profiler..but it only shows total memory used by strings as far as i know...It does not show the memory used by each string object.

Thanks

+1  A: 

Red Gate Software makes Ants Profiler which I believe will give you the information you want. It's decidedly non-free but there is a 15 day trial and depending on whether or not your lucky enough to have a budget for software at your job you might be able to buy it.

Mykroft
A: 

The mount of memory allocated is for a new someclass is sizeof(someclass) rounded up; the rounding up is probabably something like sizeof(someclass) + sizeof(void*) rounded to 32.

This won't tell you what, if any, memory someclass allocates for its members.

The best way to do this might be to replace global operator new with a wrapper that records bytes allocated. Note that as alluded to above, the bytes requested are less than the bytes actually allocated, for book-keeping and alignment reasons.

This can be done in C++, I don't know about C#.

tpdi
+1  A: 

.NET Memory Profiler is excellent It's got a 14 day trial and is pretty cheap after that. It lets you track all the instances you have, graph them and see how much memory each is taking. It gives you a tremendous amount of insight into exactly what is happening in your application.

JP Alioto
+1 for .NET memory profiler. It's an awesome tool.
Sam Saffron
A: 

The free, extremely powerful and fairly tricky way of doing this is with Windbg + SOS

This blog post should be enough to start you off.

Sam Saffron
But,Please explain how or point me to a link where its explained...Thanks
Josh
A: 

CLR profiler is free and can do this. It has a learning curve, but comes with the documentation you will need.

scottm