静态对象被固定并且不能被GC重新分配?
我的类中有一个静态变量,当我通过 Windbg/sos 查看该对象时,它显示该对象已固定。我认为这意味着 GC 无法在其他地方分配该对象。知道为什么静态变量被视为固定变量吗?
这是我的类中该变量的声明
namespace ConsoleApplication1
{
class Program
{
static string Name = "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
}
,这是 Windbg 的输出形式
0:004> !do 0231bb70
Name: System.String
MethodTable: 6c97f92c
EEClass: 6c6b8ba0
Size: 418(0x1a2) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String: Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Fields:
MT Field Offset Type VT Attr Value Name
6c9828f8 4000103 4 System.Int32 1 instance 202 m_stringLength
6c981d48 4000104 8 System.Char 1 instance 41 m_firstChar
6c97f92c 4000105 8 System.String 0 shared static Empty
>> Domain:Value 004f6588:02311228 <<
0:004> !gcroot 0231bb70
Scan Thread 0 OSTHread 2824
Scan Thread 2 OSTHread 1ae0
DOMAIN(004F6588):HANDLE(Pinned):1613f4:Root: 03312020(System.Object[])->
0231bb70(System.String)
DOMAIN(004F6588):HANDLE(Pinned):1613fc:Root: 03311010(System.Object[])->
0231bb70(System.String)
。在 !gcroot 的输出中,它显示为 HANDLE(PINNED)。我误读了这个输出吗?
I have a static variable in my class, when I look at this object via windbg/sos, it shows this object as Pinned. I assume that this mean GC cannot allocate this object anywhere else. Any ideas why static variables are treated as pinned?
Here is the declaration of this variable in my class
namespace ConsoleApplication1
{
class Program
{
static string Name = "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
}
and here is the output form windbg
0:004> !do 0231bb70
Name: System.String
MethodTable: 6c97f92c
EEClass: 6c6b8ba0
Size: 418(0x1a2) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String: Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Fields:
MT Field Offset Type VT Attr Value Name
6c9828f8 4000103 4 System.Int32 1 instance 202 m_stringLength
6c981d48 4000104 8 System.Char 1 instance 41 m_firstChar
6c97f92c 4000105 8 System.String 0 shared static Empty
>> Domain:Value 004f6588:02311228 <<
0:004> !gcroot 0231bb70
Scan Thread 0 OSTHread 2824
Scan Thread 2 OSTHread 1ae0
DOMAIN(004F6588):HANDLE(Pinned):1613f4:Root: 03312020(System.Object[])->
0231bb70(System.String)
DOMAIN(004F6588):HANDLE(Pinned):1613fc:Root: 03311010(System.Object[])->
0231bb70(System.String)
See in the output of !gcroot this is displayed as HANDLE(PINNED). Am I misreading this output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串被固定的原因不是因为您有静态引用,而是因为您有字符串文字。默认情况下,字符串文字是固定的,因此您会看到它们是固定的。另请参阅此问题。
The reason the string is pinned is not because you have a static reference, but because you have a string literal. String literals are interned by default, and thus you'll see them as pinned. Please see this question as well.