任何可以限制内存缓存的内存使用的Java缓存,而不仅仅是实例计数? [英] Any Java caches that can limit memory usage of in-memory cache, not just instance count?

查看:162
本文介绍了任何可以限制内存缓存的内存使用的Java缓存,而不仅仅是实例计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的内存(和进程中)缓存,用于查询数据的短期缓存(但短期意义超出请求/响应,即会话边界)。
EhCache可能会工作,但看起来好像它可能不提供我需要的东西:限制不是缓存的对象数量,而是(大致)限制缓存数据消耗的内存量。

I am looking for a simple in-memory (and in-process) cache for short-term caching of query data (but short-term meaning beyond request/response, i.e. session boundary). EhCache would probably work, but it looks as if it might not offer one thing that I need: limits not on number of objects cached, but (approximate) limit on amount of memory consumed by cached data.

我知道很难在没有序列化的情况下找出给定对象的确切内存使用情况(我希望在一般情况下避免由于其缓慢而导致我的使用目的失败),以及我不得不自己提供大小估计。

I understand that it is hard to figure out exact memory usage for given object without serialization (which I want to avoid in general case due to its slowness defeats the purpose for my uses), and I am fine with having to provide size estimate myself.

所以:是否有一个简单的开源java缓存,允许定义缓存对象的权重,以限制数量缓存的东西?

So: is there a simple open source java cache that allows for defining "weight" of cached objects, to limit amount of things cached?

EDIT(2010年11月):对于它的价值,有一个名为 Java CacheMate 试图解决这个问题,以及其他一些改进的想法(多级内存进程内缓存)

EDIT (Nov 2010): For what it's worth, there is a new project called Java CacheMate that tries to tackle this issue, along with some other improvement ideas (multi-level in-memory in-process caching)

推荐答案

我同意保罗的意见s通常通过使用软参考缓存来解决,尽管它可能比您更喜欢早于条目。通常可接受的解决方案是使用驱逐到软缓存的普通缓存,并在可能的情况下恢复未命中的条目。这种受害者缓存方法非常有效,如果可用内存可用,则可以获得较低的条件但是可以获得额外的好处。

I agree with Paul that this is often solved by using a soft reference cache, though it may evict entries earlier than you prefer. A usually acceptable solution is to use a normal cache that evicts to the soft cache, and recovers entries on a miss if possible. This victim caching approach works pretty well, giving you a lower bar but extra benefit if free memory is available.

可以通过启用Java代理来确定内存大小,以及使用SizeOf实用程序时使用非常简单( http://sourceforge.net/projects/sizeof ) 。我只是将它用于调试目的,我建议在将其用于正常使用之前对开销进行基准测试。

The memory size can be determined by enabling the Java agent, and usage is pretty simple when using the SizeOf utility (http://sourceforge.net/projects/sizeof). I've only used this for debugging purposes, and I'd recommend benchmarking the overhead before adopting it for normal usage.

在我的缓存库中,我打算添加实现核心算法后插入评估程序的能力。这样,您可以将集合存储为值,但将缓存绑定为所有集合大小的总和。我已经看到无限集合,因为缓存中的值会导致OutOfMemoryExceptions,因此控制非常方便。

In my caching library, I am planning on adding the ability to plug in a evaluator once the core algorithm is implemented. This way you could store a collection as the value, but bound the cache by the sum of all collection sizes. I have seen unbounded collections as values in caches cause OutOfMemoryExceptions, so having control is quite handy.

如果你真的需要这个,我建议不要,我们可以增强我当前的实现以支持这一点。你可以给我发电子邮件,ben.manes-at-gmail.com。

If you really need this, and I'd advise not to, we could enhance my current implementation to support this. You can email me, ben.manes-at-gmail.com.

这篇关于任何可以限制内存缓存的内存使用的Java缓存,而不仅仅是实例计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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