how to check the memory usage of an object at runtime?

B

Bob

I'm trying to decide on what data types to put into my caching object (a
class with a static instance of Hashtable). I'm facing the choice of either
putting the string values or SqlParameter objects into the Hashtable.
There'll be a couple of hundred of these values. If the SqlParameter
objects don't eat up too much memory comparing to the strings, I'd very much
like to go with the SqlParameters. How can I check the memory usage of my
static class (or rather the static instance Hashtable) at runtime when it's
loaded with the data? I tried the SciTech .NET Memory Profiler but not sure
what I was looking at.

Thanks a lot
Bob
 
J

Jon Skeet [C# MVP]

Bob said:
I'm trying to decide on what data types to put into my caching object (a
class with a static instance of Hashtable). I'm facing the choice of either
putting the string values or SqlParameter objects into the Hashtable.
There'll be a couple of hundred of these values. If the SqlParameter
objects don't eat up too much memory comparing to the strings, I'd very much
like to go with the SqlParameters. How can I check the memory usage of my
static class (or rather the static instance Hashtable) at runtime when it's
loaded with the data? I tried the SciTech .NET Memory Profiler but not sure
what I was looking at.

I don't know the details of SqlParameter, but I very much doubt that
having a couple of hundred of them would make any significant
difference.
 
W

WJ

You may want to use the Size of the class to determine the amt. of ram your
object use.

John
 
W

Willy Denoyette [MVP]

WJ said:
You may want to use the Size of the class to determine the amt. of ram
your object use.

John

And how do you think you can get at the size of the class at run-time?

Willy.
 
W

Willy Denoyette [MVP]

This is not what I call at run-time! You can't call profiling API's from
managed code, you can only attach a profiler to a running managed process.

Willy.
 
A

AlexS

Somehow I can't agree with "can't call". But this is not the issue, right?
If you attach before object is created and then check heap and allocations
after you create and use object, doesn't this give you the required
information?
 
D

Dejan Lukovic

Hi Bob,

there is posibility to check object size at runtime by simply using SOS
debugger. Of course, you can use it from IDE or using "Debugging Tools for
Windows" (ADPPlus).
In both cases you should load SOS debugger (.load sos.dll) and execute
commands like
!dumpheap -type <your class>
pick memory address of that class and get
!objsize <address>
You will get your values.

There is an great article on similar topic at
http://www.csharphelp.com/archives4/archive622.html to start with this.

Of course you should take a look about SOS debugger at Microsoft site.

Best regards,
Dejan Lukovic
 
W

Willy Denoyette [MVP]

Inline

Willy.

AlexS said:
Somehow I can't agree with "can't call". But this is not the issue, right?
If you attach before object is created and then check heap and allocations
after you create and use object, doesn't this give you the required
information?

Sure, but this is what the OP did with the SciTec profiler, and this didn't
seem to fit the bill.
<snip
I tried the SciTech .NET Memory Profiler but not sure
what I was looking at.
/snip>

I guess what the OP wants is to check the object size from within his code.
Note that a profiler only gives you the size of your individual objects not
the size of the object graphs, that means if the object is a container, it
only gives you the (actual) size of the container NOT including the
contained objects. So to get the real object size you have to walk the graph
and add the sizes of the sub-objects to the size of the container, which is
not a trivial task. The final result is the total size of an object graph
that might change over time depending on the size of the subobjects (strings
f.i), so in fact it only gives you a raw estimate, something that's not
worth the trouble IMO.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top