Stellen Sie eine FrageStellen Sie eine Frage
 

Beantwortetsizeof for reference types?

Antworten

  • Donnerstag, 11. Dezember 2008 16:13JaysonGo TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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

Alle Antworten

  • Donnerstag, 11. Dezember 2008 15:01Stephen Cleary TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Parker -

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

           -Steve
  • Donnerstag, 11. Dezember 2008 16:13JaysonGo TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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

  • Donnerstag, 11. Dezember 2008 16:30Parker Hillius TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Wow, thank you very much Jayson.
    - Parker Hillius
  • Donnerstag, 11. Dezember 2008 16:32JaysonGo TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    You're very welcome.  And thank you for posting the question... I learned something today, myself! :)
  • Donnerstag, 11. Dezember 2008 16:59KomplexoR TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    JaysonGo said:

    Anyone why the blogs link refer to SOS as Strike?


    Son of Strike -> SOS

    no need for a sig