Odeslat dotazOdeslat dotaz
 

Odpovědětsizeof for reference types?

Odpovědi

  • 11. prosince 2008 16:13JaysonGo Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    Hi Parker,

    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.

    Summary of what I found:
    You can use the SOS Debugging Extension [1] to find out the size of your object either in WinDbg or in Visual Studio.  The general steps to doing so is this:

    1. Enable your project for unmanaged debugging 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.
    2. Load the SOS.dll into Visual Studio.  To do this, in the Immediate Window, type !load sos.dll.  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.
    2. While in a breakpoint, determine the address of your object.  You can do this by opening up a Memory window (Debug/Windows/Memory/Memory1).  From your code editor, highlight your variable and drag it to the memory window.  At the top should be the address of the object.
    3. Go to Immediate Window and type !ObjSize <address>, and it should output something like:

    !ObjSize 0x013689e0
    sizeof(013689e0) = 16 ( 0x10) bytes (System.Object[])


    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?

    I hope that helps.
    Jayson

    [1] http://msdn.microsoft.com/en-us/library/bb190764.aspx
    [2] http://channel9.msdn.com/forums/TechOff/225836-Measuring-the-size-of-a-net-Instance/#Page=1
    [3] http://blogs.msdn.com/jfoscoding/archive/2006/07/19/670664.aspx
    [4] http://blogs.msdn.com/jfoscoding/archive/2006/07/21/673537.aspx

Všechny reakce

  • 11. prosince 2008 15:01Stephen Cleary Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Parker -

    Not directly. You can use Marshal.SizeOf to determine the size of an unmanaged equivalent type.

           -Steve
  • 11. prosince 2008 16:13JaysonGo Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    Hi Parker,

    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.

    Summary of what I found:
    You can use the SOS Debugging Extension [1] to find out the size of your object either in WinDbg or in Visual Studio.  The general steps to doing so is this:

    1. Enable your project for unmanaged debugging 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.
    2. Load the SOS.dll into Visual Studio.  To do this, in the Immediate Window, type !load sos.dll.  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.
    2. While in a breakpoint, determine the address of your object.  You can do this by opening up a Memory window (Debug/Windows/Memory/Memory1).  From your code editor, highlight your variable and drag it to the memory window.  At the top should be the address of the object.
    3. Go to Immediate Window and type !ObjSize <address>, and it should output something like:

    !ObjSize 0x013689e0
    sizeof(013689e0) = 16 ( 0x10) bytes (System.Object[])


    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?

    I hope that helps.
    Jayson

    [1] http://msdn.microsoft.com/en-us/library/bb190764.aspx
    [2] http://channel9.msdn.com/forums/TechOff/225836-Measuring-the-size-of-a-net-Instance/#Page=1
    [3] http://blogs.msdn.com/jfoscoding/archive/2006/07/19/670664.aspx
    [4] http://blogs.msdn.com/jfoscoding/archive/2006/07/21/673537.aspx

  • 11. prosince 2008 16:30Parker Hillius Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Wow, thank you very much Jayson.
    - Parker Hillius
  • 11. prosince 2008 16:32JaysonGo Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    You're very welcome.  And thank you for posting the question... I learned something today, myself! :)
  • 11. prosince 2008 16:59KomplexoR Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    JaysonGo said:

    Anyone why the blogs link refer to SOS as Strike?


    Son of Strike -> SOS

    no need for a sig