谷歌普通缓存

系统教程 行业动态 更新时间:2024-06-14 16:59:46
谷歌普通缓存 - maximumSize(和其他“可选”设置)的默认值 - 希望使用所有“可用”内存的缓存(google common cache - default value of maximumSize (and other “optional” settings) - want a cache that uses all “available” memory)

我刚刚通过搜索缓存API发现了Guava (它完全适合我的需求)。 但是在阅读wiki和Javadoc时出现了一个问题 - CacheBuilder可以采用什么设置的默认值? Javadoc声明“这些特性都是可选的”和“使用默认设置构造一个新的CacheBuilder实例,包括强键,强值和任何类型的自动驱逐。”

在我看来, maximumSize一个很好的默认值是相对于Runtime.getRuntime().freeMemory();

最后,我想要一个使用给定系统上可用内存的缓存。 所以我需要一个驱逐策略,询问有多少freeMemory()可用(可能相对于Runtime.getRuntime().maxMemory() )

I just found Guava by searching for a cache API (it fits perfectly for my needs). But one question arose on reading the wiki and Javadoc - what are the default values of settings the CacheBuilder can take? The Javadoc states "These features are all optional" and "Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind."

In my opinion, a good default for maximumSize would be relative to Runtime.getRuntime().freeMemory();

At the end I want a cache that uses the memory available on a given system. So I need an eviction strategy that asks how much freeMemory() is available (probably relative to Runtime.getRuntime().maxMemory())

最满意答案

我让我质疑同样的事情,在网上找不到任何东西。 所以我做了这个非常原始的测试。 我编写了一段代码,用最基本的设置创建一个LocalCache(没有最大大小,没有驱逐策略,没有),并在无限循环中将东西放入缓存中。 并通过VisualVm监视它以检查堆使用情况。

import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.util.concurrent.TimeUnit; public class CacheTest { public static void main(String[] args) { Cache<String, String> cache = CacheBuilder.newBuilder().build(); int counter = 0; while(true){ cache.put("key"+counter++,"value"); System.out.println("size:"+cache.size()); } } }

正如您从下图中看到的那样,内存使用量增长到最大可用空间并且变得不变。 我等了几分钟,没有发生OutOfMemoryError。 发生的事情是,几秒钟之后,一个新条目被添加到地图中,因此将来可能会出现错误。

堆转储

结论:您不必设置maximumSize值,但我建议您使用某种驱逐策略(expireAfterAccess或expireAfterWrite)来清理缓存并避免出现OutOfMemoryError。 并且还要避免降低缓存的性能。

I got me questioning the same thing and could not find anything on the web for that. So I made this very primitive test. I wrote a piece of code that creates a LocalCache with the most basic setup (no maximum size, no eviction policies, nothing) and in an infinite loop puts stuff in the cache. And monitored it through VisualVm to check the heap usage.

import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.util.concurrent.TimeUnit; public class CacheTest { public static void main(String[] args) { Cache<String, String> cache = CacheBuilder.newBuilder().build(); int counter = 0; while(true){ cache.put("key"+counter++,"value"); System.out.println("size:"+cache.size()); } } }

As you can see from the image below, the memory usage grows to the maximum available space and becomes constant. I waited for a few minutes and no OutOfMemoryError ocurred. What happened is that after a few seconds one new entry is added to the map so there will be probably an error in the future.

Heap Dump

Conclusion: You don't have to set the maximumSize value, but I suggest you use some kind of eviction policy (expireAfterAccess or expireAfterWrite) to clean up the cache and avoid an OutOfMemoryError. And also to avoid degrading the performance of your cache.

更多推荐

本文发布于:2023-04-17 08:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/359c0c2400edc0cbd7a8afd75e12837e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!