Microsoft Developer Network >
Página principal de foros
>
.NET Base Class Library
>
sizeof for reference types?
sizeof for reference types?
- Is there any easy way to determine how much space a variable is taking up?
- Parker Hillius
Respuestas
- 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- Marcado como respuestaParker Hillius jueves, 11 de diciembre de 2008 16:30
Todas las respuestas
- Parker -
Not directly. You can use Marshal.SizeOf to determine the size of an unmanaged equivalent type.
-Steve - 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- Marcado como respuestaParker Hillius jueves, 11 de diciembre de 2008 16:30
- Wow, thank you very much Jayson.
- Parker Hillius - You're very welcome. And thank you for posting the question... I learned something today, myself! :)
- JaysonGo said:
Anyone why the blogs link refer to SOS as Strike?
Son of Strike -> SOS
no need for a sig

