在番石榴的地图中被驱逐的懒惰 [英] Laziness of eviction in Guava's maps

查看:104
本文介绍了在番石榴的地图中被驱逐的懒惰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前的地图驱逐算法非常懒惰。看起来只有当数据结构被访问时,过期的对象才会被驱逐出去。



例如,从地址到索引器的映射定义为:

  ConcurrentMap< Address,Indexer> indexers = new MapMaker()
.expireAfterAccess(EXPIRATION,TimeUnit.SECONDS)
.evictionListener(new IndexEvicted())
.makeMap();

导致相当令人惊讶的模式:while containsKey()给定的地址返回false,紧接着那个地址的索引器被驱逐。



什么是推荐的方法来使清理过程更实时?即删除接近实际到期时间的对象。



更新:我想更进一步阐明我的意思。对于上面的示例,EXPIRATION为10秒,我希望在上次访问后10秒内看到插入的对象被驱逐。现在没有发生 - 地图必须以某种方式用于开始驱逐。如果地图完全没有使用,对象可以在那里停留多年。

为了及时驱逐,番石榴需要实现某种后台线程或定时重复任务。这样做会使地图在J2EE环境或安全策略阻止线程繁殖的环境中更加沉重和难以使用。



如果你关心关于及时驱逐,建立你自己的定时线程,触及地图。



另一方面,我确实认为有垃圾收集器触发驱逐会很好。 ..例如通过使用SoftReference和终结器。 (是的,我知道终结者主要是邪恶的,我只是建议一个可选的最后一招策略。)


Current eviction algorithm for maps is quite lazy. It looks like expired objects are evicted only when the data structure is accessed.

For example, the map from addresses to indexers defined as:

ConcurrentMap<Address, Indexer> indexers = new MapMaker()
  .expireAfterAccess( EXPIRATION, TimeUnit.SECONDS)
  .evictionListener( new IndexEvicted())
  .makeMap();

leads to quite surprising pattern: while containsKey() for the given address returns false, immediately after that indexer for that address is evicted.

What would be the recommended approach to make the clean up process more real-time? I.e. to remove the objects close to the actual expiration time.

Update: I'd like to more clarify what I mean by real-time. For the example above, EXPIRATION being 10 seconds, I'd like to see the inserted object evicted in 10 seconds after last access. That is not happening now - the map must be used somehow to start the eviction. If the map is completely unused, the object can stay there for years.

解决方案

In order to have timely eviction, Guava would need to implement some sort of background thread or a timed recurring task. Doing so would make the map more heavy weight and harder to use in environments like J2EE or environments where the security policy prevents threads from being spawned willy-nilly.

If you care about timely eviction, set up your own timed thread that touches the map.

On the flip side, I do agree that having a garbage-collector triggered eviction would be nice... for example by using a SoftReference and a finalizer. (Yes, I know finalizers are mostly evil, I am simply suggesting an optional last-resort strategy.)

这篇关于在番石榴的地图中被驱逐的懒惰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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