【发布时间】:2012-10-08 06:04:59
【问题描述】:
可能重复:
Weak references
这东西是干什么用的?
这似乎是一种创建 GC 可以提早收集的对象的方法,如果提早收集,我应该重新创建它。这听起来像是缓存,但其他 StackOverflow questions say that this is a lousy cache strategy because the GC in practice will GC your object very eager and fast、(another Q that says weakreference make poor caches) 有点像使用 ASP.NET 的缓存并将其设置为非常低的缓存驱逐时间限制。
背景:我最近在阅读 TraceSource 的 CLR 代码。在构造函数中,发生的第一件事是将 WeakReference(this) 添加到静态字典中。奇怪的是,只有当 TraceSource 重新读取弱引用字典中每个 TraceSource 的配置文件时,它才会在 Refresh 方法中使用,但前提是它还没有被 GC 处理。还有一个StackOverflow question indicates this causes memory leaks。
所以从那以后我一直在阅读有关 WeakReferences 的文章,并且越来越困惑。
【问题讨论】:
-
@DarthVader 我不确定 JVM 行为是否代表 CLR 行为。
标签: c# weak-references