相当于.net中的SoftReference?
我熟悉 WeakReference
,但我正在寻找一种仅在内存不足时才清除的引用类型,而不是每次 gc 运行时才清除(就像 Java 的 <代码>软引用)。 我正在寻找一种实现内存敏感缓存的方法。
I am familiar with WeakReference
, but I am looking for a reference type that is cleared only when memory is low, not simply every time when the gc runs (just like Java's SoftReference
). I'm looking for a way to implement a memory-sensitive cache.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ASP.NET 缓存为您提供了所需的内存敏感行为,但缺点是所有内容都需要唯一的键。 但是,您应该能够保存对放置在 ASP.NET 缓存中的对象的 WeakReference。 缓存的强引用将阻止 GC,直到缓存决定需要清理它以释放内存。 WeakReference 使您可以访问对象,而无需使用缓存键进行查找。
您可以通过扩展 WeakReference 来创建 SoftReference 类,
您还需要重写 Target 上的 setter 以确保任何新目标都进入缓存。
The ASP.NET cache gives you the memory-sensitive behaviour you want, with the drawback that everything needs a unique key. However, you should be able to hold a WeakReference to an object that you've placed in the ASP.NET cache. The cache's strong reference will keep the GC at bay until the cache decides that it needs to be scavenged to free memory. The WeakReference gives you access to the object without doing a lookup with the cache key.
You could create a SoftReference class by extending WeakReference along the lines of
You'd also need to override the setter on Target to make sure that any new target goes into the cache.
也许是 ASP.NET Cache 类 (System.Web. Caching.Cache)可能有助于实现你想要的? 如果内存不足,它会自动删除对象:
这里有一篇文章,介绍如何在 Windows 窗体应用程序中使用 Cache 类。
Maybe the ASP.NET Cache class (System.Web.Caching.Cache) might help achieve what you want? It automatically remove objects if memory gets low:
Here's an article that shows how to use the Cache class in a windows forms application.
不,没有同等的东西。
WeakReference
无法完成这项工作是否有特殊原因?这是与您类似的问题:
为什么 .NET 不像 Java 那样拥有 SoftReference 和 WeakReference?
No there isn't an equivalent. Is there a particular reason why
WeakReference
won't do the job?Here is a similar question to yours:
Why doesn't .NET have a SoftReference as well as a WeakReference, like Java?
除了 ASP.NET 缓存之外,还有来自 Microsoft 模式和实践组的缓存应用程序块。
http://msdn.microsoft.com/en-us/library/cc309502。 ASPX
In addition to the ASP.NET Cache, there is the Caching Application Block from the Microsoft Patterns and Practices group.
http://msdn.microsoft.com/en-us/library/cc309502.aspx
虽然 SoftReference 看起来像是实现内存缓存的一种便捷方法,但它要求 Java 运行时做出某种任意的确定,以判断保留对象的好处是否超过存储对象的成本。 不幸的是,运行时关于保留对象的实际成本的信息有限(请记住,实际成本可能包括应用程序的内存使用对其他应用程序的影响),并且实际上没有关于保留对象的好处的信息。
如果即使不存在对对象的外部强引用,也值得保留一个对象,那么缓存应该保留对其的强引用(至少只要它看起来值得)。 如果将对象保留在缓存中的好处只会在外部引用存在的情况下延伸(例如,因为生成实例很便宜,但是拥有两个保存相同数据的逻辑实体使用相同的实例来保存它们将有助于这些实体之间的比较),应该使用弱引用。
顺便说一句,如果我有我的建议,.net 将支持另一种我在任何平台上都没有见过的引用:“其他人感兴趣”的引用,它将与弱引用类型结合使用。 “其他人感兴趣”的引用可以用作强引用,但如果对其目标的唯一强引用是“其他人感兴趣”,则经过适当配置的
WeakReference
将无效。 在使用并发 GC 时,如果弱事件处理程序会重复生成对其目标的强引用,这样的概念可以提高效率。 如果没有人真正对事件处理程序对其目标执行的操作感兴趣,那么处理程序可以取消订阅将是可取的。Although a
SoftReference
might seem like a convenient way to implement memory caching, it requires the Java runtime to make a somewhat arbitrary determination as to whether the benefit to keeping an object around exceeds the cost of storing it. Unfortunately, the runtime has limited information about the real cost of keeping an object around (bearing in mind that the real cost may include the impact of a application's memory usage on other applications), and practically no information about the benefit to keeping the object around.If it will be worthwhile to keep an object around even when no outside strong references to it exist, a cache should keep a strong reference to it (at least as long as it seems worthwile). If the benefit from keeping the object in the cache will only extend as long as an outside reference exists (e.g. because producing instances is cheap, but having two logical entities that hold identical data use the same instance to hold them would facilitate comparisons between those entities), one should use a
WeakReference
.Incidentally, if I had my druthers, .net would support another kind of reference which I've not seen in any platform: an "of interest to someone else" reference, which would be used in conjunction with a type of
WeakReference
. An "of interest to someone else" reference could be used as a strong reference, but a suitably-configuredWeakReference
would be invalidated if the only strong references to its target were "of interest to someone else". Such a concept could improve efficiency when using a concurrent GC, in cases where a weak-event handler would repeatedly generate a strong reference to its target. If nobody's really interested in what the event handler is doing with its target, it would be desirable if the handler could get unsubscribed.