Pet*_*yer 36 .net garbage-collection
我理解System.WeakReference的作用,但我似乎无法掌握的是它可能有用的实际例子.在我看来,班级本身就是一个黑客.在我看来,还有其他更好的解决问题的方法,在我看到的例子中使用了WeakReference.你真正需要使用WeakReference的典型例子是什么?我们不是试图获得更远的远离这种类型的行为,并使用这个类的?
Jud*_*ngo 46
一个有用的例子是运行DB4O面向对象数据库的人.在那里,WeakReferences被用作一种轻量级缓存:它只会在您的应用程序执行时将对象保留在内存中,从而允许您将真正的缓存放在顶部.
另一个用途是在弱事件处理程序的实现中.目前,.NET应用程序中的一个重要内存泄漏源是忘记删除事件处理程序.例如
public MyForm()
{
MyApplication.Foo += someHandler;
}
Run Code Online (Sandbox Code Playgroud)
看到问题?在上面的代码片段中,只要MyApplication在内存中存活,MyForm将永远保存在内存中.创建10个MyForms,关闭它们,你的10个MyForms仍将在内存中,由事件处理程序保持活着.
输入WeakReference.您可以使用WeakReferences构建一个弱事件处理程序,以便someHandler是MyApplication.Foo的弱事件处理程序,从而修复内存泄漏!
这不仅仅是理论.来自DidItWith.NET博客的Dustin Campbell发布了一个使用System.WeakReference 的弱事件处理程序的实现.
Mar*_*ade 13
我用它来实现一个缓存,其中未使用的条目被自动垃圾收集:
class Cache<TKey,TValue> : IEnumerable<KeyValuePair<TKey,TValue>>
{ Dictionary<TKey,WeakReference> dict = new Dictionary<TKey,WeakReference>();
public TValue this[TKey key]
{ get {lock(dict){ return getInternal(key);}}
set {lock(dict){ setInteral(key,value);}}
}
void setInteral(TKey key, TValue val)
{ if (dict.ContainsKey(key)) dict[key].Target = val;
else dict.Add(key,new WeakReference(val));
}
public void Clear() { dict.Clear(); }
/// <summary>Removes any dead weak references</summary>
/// <returns>The number of cleaned-up weak references</returns>
public int CleanUp()
{ List<TKey> toRemove = new List<TKey>(dict.Count);
foreach(KeyValuePair<TKey,WeakReference> kv in dict)
{ if (!kv.Value.IsAlive) toRemove.Add(kv.Key);
}
foreach (TKey k in toRemove) dict.Remove(k);
return toRemove.Count;
}
public bool Contains(string key)
{ lock (dict) { return containsInternal(key); }
}
bool containsInternal(TKey key)
{ return (dict.ContainsKey(key) && dict[key].IsAlive);
}
public bool Exists(Predicate<TValue> match)
{ if (match==null) throw new ArgumentNullException("match");
lock (dict)
{ foreach (WeakReference weakref in dict.Values)
{ if ( weakref.IsAlive
&& match((TValue) weakref.Target)) return true;
}
}
return false;
}
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6015 次 |
| 最近记录: |