.NET 将字符串值放在哪里?
我正在使用 SOS 调试扩展 dll 来检查 String 类型的内存布局,下面是结果。
!dso
ESP/REG Object Name
0015EFC0 01c6b9cc System.String hello,world
!do 01c6b9cc
Name: System.String
MethodTable: 6de3f9ac
EEClass: 6db78bb0
Size: 36(0x24) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089>\mscorlib.dll
String: hello,world
Fields:
MT Field Offset Type VT Attr Value Name
6de42978 40000ed 4 System.Int32 1 instance 11 m_stringLength
6de41dc8 40000ee 8 System.Char 1 instance 68 m_firstChar
6de3f9ac 40000ef 8 System.String 0 shared static Empty
>> Domain:Value 00331488:01c61228 <<
现在我想知道,字符串值“hello world”到底存储在哪里?
谢谢。
I am using SOS debug extension dll to check the memory layout of a String type, and below is the result.
!dso
ESP/REG Object Name
0015EFC0 01c6b9cc System.String hello,world
!do 01c6b9cc
Name: System.String
MethodTable: 6de3f9ac
EEClass: 6db78bb0
Size: 36(0x24) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089>\mscorlib.dll
String: hello,world
Fields:
MT Field Offset Type VT Attr Value Name
6de42978 40000ed 4 System.Int32 1 instance 11 m_stringLength
6de41dc8 40000ee 8 System.Char 1 instance 68 m_firstChar
6de3f9ac 40000ef 8 System.String 0 shared static Empty
>> Domain:Value 00331488:01c61228 <<
Now I am wondering, where exactly is the string value "hello world" stored?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 m_firstChar。堆分配足够大,可以容纳整个字符串,而不仅仅是第一个字符。在 Visual Studio 中也很容易看到:
当断点命中时,使用 Debug + Windows + Memory + Memory1。在地址框中输入 s。您将看到:
这是针对 .NET 3.5 SP1 的。在 .NET 4.0 及更高版本中,您将看不到 m_arrayLength 成员,该字段已被删除。
At m_firstChar. The heap allocation is large enough to fit the entire string, not just the first character. Easy to see in Visual Studio as well:
When the breakpoint hits, use Debug + Windows + Memory + Memory1. In the Address box type s. You'll see:
This is for .NET 3.5 SP1. You won't see the m_arrayLength member in .NET 4.0 and up, the field was removed.
与 C“字符串”类似,它存储在从
m_firstChar
开始的m_stringLength
字节中,这是一个不安全的指针,而不是实际的字符。不过,C# 使用长度前缀字符串而不是null
分隔字符串。也就是说,CLR 的美妙之处在于您无需关心。这怎么变成一个问题了?
Like a C "string", it's stored in the
m_stringLength
bytes starting atm_firstChar
which is an unsafe pointer, not an actual character. C# uses a length prefixed string rather than anull
delimited one though.That said, the beauty of the CLR is that you don't need to care. How has this become an issue?