google通用缓存-maximumSize的默认值(和其他“可选"设置)-希望使用所有“可用"缓存的缓存.记忆 [英] google common cache - default value of maximumSize (and other "optional" settings) - want a cache that uses all "available" memory

查看:1917
本文介绍了google通用缓存-maximumSize的默认值(和其他“可选"设置)-希望使用所有“可用"缓存的缓存.记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通过搜索缓存API找到了 Guava (它非常适合我的需求). 但是,在阅读 Wiki Javadoc 指出这些功能都是可选的"和使用默认设置构造一个新的CacheBuilder实例,包括强键,强值,并且不会自动驱逐任何形式."

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."

我认为maximumSize的默认值相对于Runtime.getRuntime().freeMemory();

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

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

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对其进行监视以检查堆使用情况.

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());
        }
    }
}

从下图可以看到,内存使用量增长到最大可用空间并保持不变.我等待了几分钟,没有发生OutOfMemoryError.发生的事情是,几秒钟后,一个新条目被添加到地图上,因此将来可能会出现错误.

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.

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

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.

这篇关于google通用缓存-maximumSize的默认值(和其他“可选"设置)-希望使用所有“可用"缓存的缓存.记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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