Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Last active April 8, 2018 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rednaxelafx/7244863 to your computer and use it in GitHub Desktop.
Save rednaxelafx/7244863 to your computer and use it in GitHub Desktop.
Explore 32-bit .NET 4.0 System.String's memory layout. ObjSize command gives the size of the whole object (with padding due to alignment), including the object header (at negative offset just before the object). DumpObj command gives the size of object without the padding bytes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
var s = "foobar";
Console.WriteLine(s); // breakpoint here
}
}
}
!load sos
extension C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll loaded
!help
PDB symbol for clr.dll not loaded
-------------------------------------------------------------------------------
SOS is a debugger extension DLL designed to aid in the debugging of managed
programs. Functions are listed by category, then roughly in order of
importance. Shortcut names for popular functions are listed in parenthesis.
Type "!help <functionname>" for detailed info on that function.
Object Inspection Examining code and stacks
----------------------------- -----------------------------
DumpObj (do) Threads
DumpArray (da) ThreadState
DumpStackObjects (dso) IP2MD
DumpHeap U
DumpVC DumpStack
GCRoot EEStack
ObjSize CLRStack
FinalizeQueue GCInfo
PrintException (pe) EHInfo
TraverseHeap BPMD
COMState
Examining CLR data structures Diagnostic Utilities
----------------------------- -----------------------------
DumpDomain VerifyHeap
EEHeap VerifyObj
Name2EE FindRoots
SyncBlk HeapStat
DumpMT GCWhere
DumpClass ListNearObj (lno)
DumpMD GCHandles
Token2EE GCHandleLeaks
EEVersion FinalizeQueue (fq)
DumpModule FindAppDomain
ThreadPool SaveModule
DumpAssembly ProcInfo
DumpSigElem StopOnException (soe)
DumpRuntimeTypes DumpLog
DumpSig VMMap
RCWCleanupList VMStat
DumpIL MinidumpMode
DumpRCW AnalyzeOOM (ao)
DumpCCW
Examining the GC history Other
----------------------------- -----------------------------
HistInit FAQ
HistRoot
HistObj
HistObjFind
HistClear
!do 0x024E2280
Name: System.String
MethodTable: 5632acc0
EEClass: 55f3486c
Size: 26(0x1a) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String: foobar
Fields:
MT Field Offset Type VT Attr Value Name
5632c480 40000aa 4 System.Int32 1 instance 6 m_stringLength
5632b6b8 40000ab 8 System.Char 1 instance 66 m_firstChar
5632acc0 40000ac c System.String 0 shared static Empty
>> Domain:Value 004f5070:NotInit <<
!objsize 0x024E2280
sizeof(024e2280) = 28 (0x1c) bytes (System.String)
// Memory
// 0x024E227C 00 00 00 80 c0 ac 32 56 06 00 00 00 66 00 6f 00 6f 00 62 00 61 00 72 00 00 00 00 00 ...€??2V....f.o.o.b.a.r.....
!dumpheap
Address MT Size
024e1000 00531658 10 Free
024e100c 00531658 10 Free
024e1018 00531658 10 Free
024e1024 5632ae08 84
024e1078 5632af5c 84
024e10cc 5632afa0 84
024e1120 5632afe4 84
024e1174 5632b028 84
024e11c8 5632b028 84
024e121c 5632b060 12
024e1228 5632acc0 14
024e1238 5632b0b4 28
024e1254 5632acc0 76
024e12a0 5632acc0 108
024e130c 5632b130 112
024e137c 5632acc0 22
024e1394 5632acc0 78
024e13e4 5632b4e4 68
024e1428 5632acc0 94
024e1488 5632acc0 28
024e14a4 5632b680 16
024e14b4 5632acc0 32
024e14d4 562dab98 88
024e152c 5632acc0 76
024e1578 5632acc0 46
024e15a8 5632acc0 28
024e15c4 5632acc0 16
024e15d4 5632acc0 28
024e15f0 5632acc0 38
024e1618 5632acc0 44
024e1644 5632acc0 46
024e1674 5632acc0 34
024e1698 5632acc0 30
024e16b8 5632acc0 38
024e16e0 5632acc0 58
024e171c 5632acc0 58
024e1758 5632acc0 64
024e1798 5632acc0 52
024e17cc 5632acc0 50
024e1800 5632acc0 44
024e182c 5632acc0 44
024e1858 5632acc0 30
024e1878 5632acc0 42
024e18a4 5632acc0 56
024e18dc 5632acc0 36
024e1900 5632acc0 24
024e1918 5632acc0 106
024e1984 5632acc0 148
024e1a18 5632acc0 78
024e1a68 5632acc0 70
024e1ab0 5632acc0 82
024e1b04 5632acc0 56
024e1b3c 5632b938 36
024e1b60 5632de0c 20
024e1b74 5632de68 32
024e1b94 5632b7b0 40
024e1bbc 55f42268 48
024e1bec 5632be78 28
024e1c08 5632be78 28
024e1c24 5632be78 28
024e1c40 5632be78 28
024e1c5c 5632be78 28
024e1c78 5632be78 28
024e1c94 5632be78 28
024e1cb0 5632be78 28
024e1ccc 5632be78 28
024e1ce8 5632be78 28
024e1d04 5632a58c 12
024e1d10 562dab98 56
024e1d48 5632be78 28
024e1d64 5632be78 28
024e1d80 5632be78 28
024e1d9c 5632be78 28
024e1db8 5632be78 28
024e1dd4 5632be78 28
024e1df0 5632be78 28
024e1e0c 5632be78 28
024e1e28 5632be78 28
024e1e44 5632be78 28
024e1e60 5632c448 300
024e1f8c 5632b060 12
024e1f98 5632c448 24
024e1fb0 5632a9f8 60
024e1fec 5632c448 40
024e2014 5632a9f8 124
024e2090 5632c448 80
024e20e0 5632a9f8 284
024e21fc 5632aa64 44
024e2228 5632cb20 28
024e2244 5632dec0 16
024e2254 5632aaac 16
024e2264 5632aaf0 12
024e2270 562dab98 16
024e2280 5632acc0 26
024e229c 5632acc0 320
024e23dc 5632a368 12
024e23e8 562dab98 16
024e23f8 5632a3b8 12
024e2404 5632a414 32
024e2424 5632a414 32
024e2444 5632a414 32
024e2464 5632be78 28
024e2480 5632acc0 244
024e2574 00283ea4 32
024e2594 5632acc0 50
024e25c8 56328e54 24
024e25e0 56328e54 24
024e25f8 5632acc0 86
024e2650 5632acc0 90
024e26ac 56328e54 24
024e26c4 5632acc0 60
024e2700 5632acc0 48
024e2730 5632acc0 48
024e2760 5632acc0 58
024e279c 5632be78 28
024e27b8 5632be78 28
024e27d4 00283f88 32
034e1000 00531658 10 Free
034e1010 562dab98 4372
034e2128 562dab98 528
034e2338 562dab98 4096
034e3338 562dab98 8176
Statistics:
MT Count TotalSize Class Name
5632aaf0 1 12 System.Security.HostSecurityManager
5632a58c 1 12 System.Collections.Generic.ObjectEqualityComparer`1[[System.Type, mscorlib]]
5632a3b8 1 12 System.__Filters
5632a368 1 12 System.Reflection.Missing
5632dec0 1 16 System.Security.Policy.AssemblyEvidenceFactory
5632b680 1 16 System.Char[]
5632aaac 1 16 System.Security.Policy.Evidence+EvidenceLockHolder
5632de0c 1 20 Microsoft.Win32.SafeHandles.SafePEFileHandle
5632b060 2 24 System.Object
5632cb20 1 28 System.Reflection.RuntimeAssembly
5632b0b4 1 28 System.SharedStatics
5632de68 1 32 System.Security.Policy.PEFileEvidenceFactory
00283f88 1 32 Microsoft.VisualStudio.Debugger.Runtime.Main+_InitRuntimeDllImpl
00283ea4 1 32 Microsoft.VisualStudio.Debugger.Runtime.Main+_ThrowCrossThreadMessageException
5632b938 1 36 System.Security.PermissionSet
5632b7b0 1 40 System.Security.Policy.Evidence
00531658 4 40 Free
5632aa64 1 44 System.Threading.ReaderWriterLock
55f42268 1 48 System.Collections.Generic.Dictionary`2[[System.Type, mscorlib],[System.Security.Policy.EvidenceTypeDescriptor, mscorlib]]
5632b4e4 1 68 System.AppDomainSetup
56328e54 3 72 System.Guid
5632afe4 1 84 System.ExecutionEngineException
5632afa0 1 84 System.StackOverflowException
5632af5c 1 84 System.OutOfMemoryException
5632ae08 1 84 System.Exception
5632a414 3 96 System.Reflection.MemberFilter
5632b130 1 112 System.AppDomain
5632b028 2 168 System.Threading.ThreadAbortException
5632c448 4 444 System.Int32[]
5632a9f8 3 468 System.Collections.Generic.Dictionary`2+Entry[[System.Type, mscorlib],[System.Security.Policy.EvidenceTypeDescriptor, mscorlib]][]
5632be78 23 644 System.RuntimeType
5632acc0 47 3004 System.String
562dab98 8 17348 System.Object[]
Total 122 objects
!do 0x024e229c
Name: System.String
MethodTable: 5632acc0
EEClass: 55f3486c
Size: 320(0x140) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String: Microsoft.VisualStudio.Debugger.Runtime.Main, Microsoft.VisualStudio.Debugger.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Fields:
MT Field Offset Type VT Attr Value Name
5632c480 40000aa 4 System.Int32 1 instance 153 m_stringLength
5632b6b8 40000ab 8 System.Char 1 instance 4d m_firstChar
5632acc0 40000ac c System.String 0 shared static Empty
>> Domain:Value 004f5070:NotInit <<
// Memory
// 0x024E227C 00 00 00 ...
// 0x024E229B 00 c0 ac 32 56 99 00 00 00 4d 00 69 00 63 00 72 00 6f 00 73 00 6f 00 66 00 74 00 2e 00 56 00 .??2V?...M.i.c.r.o.s.o.f.t...V.
// 0x024E22BA 69 00 73 00 75 00 61 00 6c 00 53 00 74 00 75 00 64 00 69 00 6f 00 2e 00 44 00 65 00 62 00 75 i.s.u.a.l.S.t.u.d.i.o...D.e.b.u
// 0x024E22D9 00 67 00 67 00 65 00 72 00 2e 00 52 00 75 00 6e 00 74 00 69 00 6d 00 65 00 2e 00 4d 00 61 00 .g.g.e.r...R.u.n.t.i.m.e...M.a.
// 0x024E22F8 69 00 6e 00 2c 00 20 00 4d 00 69 00 63 00 72 00 6f 00 73 00 6f 00 66 00 74 00 2e 00 56 00 69 i.n.,. .M.i.c.r.o.s.o.f.t...V.i
// 0x024E2317 00 73 00 75 00 61 00 6c 00 53 00 74 00 75 00 64 00 69 00 6f 00 2e 00 44 00 65 00 62 00 75 00 .s.u.a.l.S.t.u.d.i.o...D.e.b.u.
// 0x024E2336 67 00 67 00 65 00 72 00 2e 00 52 00 75 00 6e 00 74 00 69 00 6d 00 65 00 2c 00 20 00 56 00 65 g.g.e.r...R.u.n.t.i.m.e.,. .V.e
// 0x024E2355 00 72 00 73 00 69 00 6f 00 6e 00 3d 00 31 00 31 00 2e 00 30 00 2e 00 30 00 2e 00 30 00 2c 00 .r.s.i.o.n.=.1.1...0...0...0.,.
// 0x024E2374 20 00 43 00 75 00 6c 00 74 00 75 00 72 00 65 00 3d 00 6e 00 65 00 75 00 74 00 72 00 61 00 6c .C.u.l.t.u.r.e.=.n.e.u.t.r.a.l
// 0x024E2393 00 2c 00 20 00 50 00 75 00 62 00 6c 00 69 00 63 00 4b 00 65 00 79 00 54 00 6f 00 6b 00 65 00 .,. .P.u.b.l.i.c.K.e.y.T.o.k.e.
// 0x024E23B2 6e 00 3d 00 62 00 30 00 33 00 66 00 35 00 66 00 37 00 66 00 31 00 31 00 64 00 35 00 30 00 61 n.=.b.0.3.f.5.f.7.f.1.1.d.5.0.a
// 0x024E23D1 00 33 00 61 00 00 00 .3.a...
!load sos
extension C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll loaded
!objsize 0x02412280
PDB symbol for clr.dll not loaded
sizeof(02412280) = 28 (0x1c) bytes (System.String)
!do 0x02412280
Name: System.String
MethodTable: 5632acc0
EEClass: 55f3486c
Size: 28(0x1c) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String: foobara
Fields:
MT Field Offset Type VT Attr Value Name
5632c480 40000aa 4 System.Int32 1 instance 7 m_stringLength
5632b6b8 40000ab 8 System.Char 1 instance 66 m_firstChar
5632acc0 40000ac c System.String 0 shared static Empty
>> Domain:Value 00725070:NotInit <<
// Memory
// 0x0241227C 00 00 00 80 c0 ac 32 56 07 00 00 00 66 00 6f 00 6f 00 62 00 61 00 72 00 61 00 00 00 ...€??2V....f.o.o.b.a.r.a...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
var sb = new StringBuilder("foobar");
var s = sb.ToString(); // breakpoint 1
Console.WriteLine(s); // breakpoint 2
}
}
}
!load sos
extension C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll loaded
!do 0247229C
PDB symbol for clr.dll not loaded
Name: System.Text.StringBuilder
MethodTable: 5632d4b4
EEClass: 55f9c598
Size: 28(0x1c) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
Fields:
MT Field Offset Type VT Attr Value Name
5632b680 40000ca 4 System.Char[] 0 instance 024722b8 m_ChunkChars
5632d4b4 40000cb 8 ...ext.StringBuilder 0 instance 00000000 m_ChunkPrevious
5632c480 40000cc c System.Int32 1 instance 6 m_ChunkLength
5632c480 40000cd 10 System.Int32 1 instance 0 m_ChunkOffset
5632c480 40000ce 14 System.Int32 1 instance 2147483647 m_MaxCapacity
!objsize 0247229C
sizeof(0247229c) = 72 (0x48) bytes (System.Text.StringBuilder)
// Memory
// 0x02472298 00 00 00 00 b4 d4 32 56 b8 22 47 02 00 00 00 00 ....??2V?"G.....
// 0x024722A8 06 00 00 00 00 00 00 00 ff ff ff 7f ............
!do 024722b8
Name: System.Char[]
MethodTable: 5632b680
EEClass: 55f9bc60
Size: 44(0x2c) bytes
Array: Rank 1, Number of elements 16, Type Char
Content: foobar..........
Fields:
None
!objsize 024722b8
sizeof(024722b8) = 44 (0x2c) bytes (System.Char[])
// Memory
// 0x024722A8 00 00 00 00 ....
// 0x024722B8 80 b6 32 56 10 00 00 00 66 00 6f 00 6f 00 62 00 €?2V....f.o.o.b.
// 0x024722C8 61 00 72 00 00 00 00 00 00 00 00 00 00 00 00 00 a.r.............
// 0x024722D8 00 00 00 00 00 00 00 00
!do 024722E4
Name: System.String
MethodTable: 5632acc0
EEClass: 55f3486c
Size: 26(0x1a) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String: foobar
Fields:
MT Field Offset Type VT Attr Value Name
5632c480 40000aa 4 System.Int32 1 instance 6 m_stringLength
5632b6b8 40000ab 8 System.Char 1 instance 66 m_firstChar
5632acc0 40000ac c System.String 0 shared static Empty
>> Domain:Value 00355070:NotInit <<
!objsize 024722E4
sizeof(024722e4) = 28 (0x1c) bytes (System.String)
// Memory
// 0x024722D8 00 00 00 00 c0 ac 32 56 ............??2V
// 0x024722E8 06 00 00 00 66 00 6f 00 6f 00 62 00 61 00 72 00 ....f.o.o.b.a.r.
// 0x024722F8 00 00 00 00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment