c# - 包装字符串的 WeakReference 会导致奇怪的行为

标签 c#

我知道我应该只对大对象使用 Wea​​kReference,但我对以下场景感到好奇:

object obj = 1; //Int32

var wk = new WeakReference(obj);

Console.WriteLine(wk.IsAlive); //Prints: True

obj = null;

GC.Collect(2, GCCollectionMode.Forced, true);

Console.WriteLine(wk.IsAlive); //Prints: false, All Rigth!

到目前为止一切正常。

看这个:

object obj = "test"; //String

var wk = new WeakReference(obj);

Console.WriteLine(wk.IsAlive); //Prints: True

obj = null;

GC.Collect(2, GCCollectionMode.Forced, true);

Console.WriteLine(wk.IsAlive); //Prints: True, Why?

这是怎么回事?

最佳答案

来自String.Intern的评论:

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

因此,还有一个不能以编程方式发布的引用。稍微更改代码以在运行时生成一个实例会得到预期的结果:

object obj = new string(new char[] { 't', 'e', 's', 't' });
var wk = new WeakReference(obj);
Console.WriteLine(wk.IsAlive); //Prints: True
obj = null;
GC.Collect(2, GCCollectionMode.Forced, true);
Console.WriteLine(wk.IsAlive); //Prints: False

关于c# - 包装字符串的 WeakReference 会导致奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30083227/

相关文章:

c# - 如何从绑定(bind)到 C# MVVM 中的 ObservableCollection 的列表中删除对象

c# - WCF 能否通过网络保持引用相等性?

c# - 学习和实践领域驱动设计,寻找一些指导

c# - 使用泛型扩展接口(interface)不可分配给父级 c#

c# - linq to xml检查 key 是否存在?

c# - .net队列系统

c# - 使用 3 个表的 Fluent NHibernate Dictionary<string, Entity>

c# - 具有自动代理登录的 WebBrowser 控件

c# - 通过套接字发送和接收多个变量数据的最佳方式

一组 (x,y) 点的 C# 数据结构