4

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

Josh
  • 13,120
  • 28
  • 112
  • 158

5 Answers5

2

.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
  • 44,379
  • 6
  • 86
  • 112
1

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
  • 12,717
  • 13
  • 43
  • 71
1

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

scottm
  • 27,151
  • 22
  • 104
  • 158
0

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
  • 33,714
  • 11
  • 78
  • 117
0

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
  • 124,587
  • 78
  • 320
  • 501