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,530
  • 29
  • 114
  • 159

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,864
  • 6
  • 88
  • 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
  • 13,077
  • 13
  • 44
  • 72
1

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

scottm
  • 27,829
  • 22
  • 107
  • 159
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
  • 34,554
  • 11
  • 80
  • 120
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
  • 128,308
  • 78
  • 326
  • 506