难道WeakReference的制作好的缓存? [英] Does WeakReference make a good cache?

查看:265
本文介绍了难道WeakReference的制作好的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用在WeakReferences到缓存的对象,使他们自动的情况下内存pressure缓存中移除缓存。我的问题是缓存对象被收集很快他们被存储在缓存中之后。缓存的64位应用程序,尽管的情况下运行,超过4gig的内存仍然可用,所有缓存对象的收集(他们通常都在那一刻存储在G2堆)。有人工诱发的进程资源管理器显示没有垃圾收集。

i have a cache that uses WeakReferences to the cached objects to make them automatically removed from the cache in case of memory pressure. My problem is that the cached objects are collected very soon after they have been stored in the cache. The cache runs in a 64-Bit application and in spite of the case that more than 4gig of memory are still available, all the cached objects are collected (they usually are stored in the G2-heap at that moment). There are no garbage collection induced manually as the process explorer shows.

我可以运用什么方法,使对象生活豆蔻更长?

What methods can i apply to make the objects live a litte longer?

推荐答案

使用在WeakReferences为引用缓存对象的主要手段是不是一个真正的好主意,因为作为乔希说,你在今后的任何行为改变的WeakReference摆布和GC。

Using WeakReferences as the primary means of referencing cached objects is not really a great idea, because as Josh said, your at the mercy of any future behavioral changes to WeakReference and the GC.

不过,如果你的缓存需要的任何一种复活的能力,在WeakReferences使用的为正在等待清洗的物品是很有用的。当一个项目达到驱逐的标准,而不是立即驱逐它,你改变它的引用弱引用。如果有什么要求它是GC'ed之前,恢复其很强的参考和对象可以再活一次。我发现这个有用的一些缓存有难predict命中率模式频繁足够的复活是有益的。

However, if your cache needs any kind of resurrection capability, use of WeakReferences for items that are pending purge is useful. When an item meets eviction criteria, rather than immediately evicting it, you change its reference to a weak reference. If anything requests it before it is GC'ed, you restore its strong reference, and the object can live again. I have found this useful for some caches that have hard to predict hit rate patterns with frequent enough "resurrections" to be beneficial.

如果您有predictable命中率的模式,那么我会forgoe的WeakReference的选项并执行明确的驱逐。

If you have predictable hit rate patterns, then I would forgoe the WeakReference option and perform explicit evictions.

这篇关于难道WeakReference的制作好的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆