Need to know the size of your .NET object?

I was doing some research on a question posted on an MSDN Forum [1] about how to determine the size of an object and I was able to find out how to do it.  This method will allow you to determine the size of your object while debugging your application with Visual Studio.
Enter the SOS Debugging Extension [2].  This library will allow you to determine the size of your managed object (among many other things).
So how do you do it?  Here are a few steps on how to do it:
1. Load your project into Visual Studio and ensure that ‘Enabled unmanaged code debugging’ is checked (see Project Settings, Debug tab).  This is unchecked by default and my guess is, there’s a reason for it, so I would suggest turning it back off when you’re finished.
2. Load the SOS Debugging Extension by opening your Immediate Window and typing .load sos.dll.  I did not have any problems doing this but if for some reason or another you don’t have the sos.dll on your machine, you may need to download and install the Debugging Tools [3].
3. Place appropriate breakpoint(s) in your application and debug it.  Once you hit a breakpoint, determine the memory location of your object.  To do this, open a Memory window by going to Debug->Windows->Memory->Memory 1.  Highlight the variable for your object and drag it into the memory window.  When you do so, the Address should be available.  Copy it to your clipboard (unless you like to typing).
4. In your immediate window, type !ObjSize <address>, where <address> is the address of your object you got from step 3.  The command should output something like:
!ObjSize 0x013689e0
sizeof(013689e0) = 16 ( 0x10) bytes (System.Object[])

And that should give you the information you wanted.

– Channel9 thread [4]
– Links posted by TommyCarlier [5][6]

Hope this helps!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s