sizeof for reference types?Is there any easy way to determine how much space a variable is taking up? <hr class="sig">- Parker Hillius© 2009 Microsoft Corporation. All rights reserved.Fri, 12 Dec 2008 16:27:09 Z7444d58d-a0c1-45f3-a979-b9092f5d2b7ehttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#7444d58d-a0c1-45f3-a979-b9092f5d2b7ehttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#7444d58d-a0c1-45f3-a979-b9092f5d2b7eParker Hilliushttp://social.msdn.microsoft.com/Profile/en-US/?user=Parker%20Hilliussizeof for reference types?Is there any easy way to determine how much space a variable is taking up? <hr class="sig">- Parker HilliusThu, 11 Dec 2008 14:19:47 Z2008-12-11T14:19:47Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#4bebb11e-6732-4e28-b15e-40e018b35d12http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#4bebb11e-6732-4e28-b15e-40e018b35d12Stephen Clearyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Stephen%20Clearysizeof for reference types? Parker -<br><br>Not directly. You can use Marshal.SizeOf to determine the size of an unmanaged equivalent type.<br><br>       -SteveThu, 11 Dec 2008 15:01:16 Z2008-12-11T15:01:16Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#ac3dd645-59a7-4768-b7fb-b92ed828621ahttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#ac3dd645-59a7-4768-b7fb-b92ed828621aJaysonGohttp://social.msdn.microsoft.com/Profile/en-US/?user=JaysonGosizeof for reference types? Hi Parker,<br><br>I was quite intriqued by your question that I wanted to do some research myself.  It turns out, there's a way to do it while debugging... and since you mentioned 'variable', I'm guessing that you're needing the size while debugging your application.<br><br>Summary of what I found:<br>You can use the <strong>SOS Debugging Extension</strong> [1] to find out the size of your object either in WinDbg or in Visual Studio.  The general steps to doing so is this:<br><br>1. Enable your project for <strong>unmanaged debugging</strong> via project properties/debug tab.  This seems to be unchecked by default and my guess is that there's a reason for it, so I would suggest only turning it on when you need to do this.<br>2. Load the SOS.dll into Visual Studio.  To do this, in the Immediate Window, type <strong>!load sos.dll</strong>.  I did this without downloading anything, so my guess is the sos.dll is already in your system.  If not, I believe the links below will have the locations to download it from.  You can do this while debugging or not.<br>2. While in a breakpoint, determine the address of your object.  You can do this by opening up a <strong>Memory window</strong> (Debug/Windows/Memory/Memory1).  From your code editor, highlight your variable and drag it to the memory window.  At the top should be the <strong>address of the object</strong>.<br>3. Go to Immediate Window and type <strong>!ObjSize &lt;address&gt;</strong>, and it should output something like:<br><br><font size=1> <p style="margin-right:0px" dir=ltr>!ObjSize 0x013689e0<br>sizeof(013689e0) = 16 ( 0x10) bytes (System.Object[])<br></font><br><br>I credit the Channel9 [2] thread that I found, and the links [3][4] provided by TommyCarlier.  Also, the MSDN link details more of the available commands from SOS.  Anyone why the blogs link refer to SOS as Strike?<br><br>I hope that helps.<br>Jayson<br><br>[1] <a href="http://msdn.microsoft.com/en-us/library/bb190764.aspx">http://msdn.microsoft.com/en-us/library/bb190764.aspx</a><br>[2] <a href="http://channel9.msdn.com/forums/TechOff/225836-Measuring-the-size-of-a-net-Instance/#Page=1">http://channel9.msdn.com/forums/TechOff/225836-Measuring-the-size-of-a-net-Instance/#Page=1</a><br>[3] <a href="http://blogs.msdn.com/jfoscoding/archive/2006/07/19/670664.aspx">http://blogs.msdn.com/jfoscoding/archive/2006/07/19/670664.aspx</a><br>[4] <a href="http://blogs.msdn.com/jfoscoding/archive/2006/07/21/673537.aspx">http://blogs.msdn.com/jfoscoding/archive/2006/07/21/673537.aspx</a></p>Thu, 11 Dec 2008 16:13:07 Z2008-12-11T16:13:07Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#ff669b5c-157b-4725-aed5-8276e63e96b8http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#ff669b5c-157b-4725-aed5-8276e63e96b8Parker Hilliushttp://social.msdn.microsoft.com/Profile/en-US/?user=Parker%20Hilliussizeof for reference types? Wow, thank you very much Jayson.<hr class="sig">- Parker HilliusThu, 11 Dec 2008 16:30:33 Z2008-12-11T16:30:33Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#6031805d-2901-4c89-b61f-6ebebd5bdd1dhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#6031805d-2901-4c89-b61f-6ebebd5bdd1dJaysonGohttp://social.msdn.microsoft.com/Profile/en-US/?user=JaysonGosizeof for reference types? You're very welcome.  And thank you for posting the question... I learned something today, myself! :)Thu, 11 Dec 2008 16:32:33 Z2008-12-11T16:32:33Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#2942586f-4d39-49a9-a05b-c39ad6a42fc1http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7444d58d-a0c1-45f3-a979-b9092f5d2b7e#2942586f-4d39-49a9-a05b-c39ad6a42fc1KomplexoRhttp://social.msdn.microsoft.com/Profile/en-US/?user=KomplexoRsizeof for reference types?<div class=quote><font class=quoteHeader>JaysonGo said:</font><p> Anyone why the blogs link refer to SOS as Strike?<br></p></div><br><strong>S</strong>on <strong>o</strong>f <strong>S</strong>trike -&gt; SOS<br><hr class="sig">no need for a sigThu, 11 Dec 2008 16:59:53 Z2008-12-11T16:59:53Z