移除键后,Guava Map Cache 会缩小吗
will Guava Map Cache shrink when keys are removed
private Cache<Object, String> cache = CacheBuilder.newBuilder()
.concurrencyLevel(4)
.expireAfterAccess(24, TimeUnit.HOURS)
.initialCapacity(1024)
.maximumSize(1_00_000)
.build();
private ConcurrentMap<Object, String> cacheMap = cache.asMap();
这是我的代码,它很简单,只是创建一个 Guava 地图缓存。
问题是,如果我从这张地图中删除()键,GC 是否能够从这张地图中收回内存?不使用 weakReference(我没有存储嵌套对象,如 Set 或 Map 等...)?
是
来自 Guava's Cache.asMap
javadoc(强调我的):
Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to the map directly affect the cache.
并且来自 Guava's Cache Explained wiki page:
At any time, you may explicitly invalidate cache entries rather than waiting for entries to be evicted.
您可能想要检查整个 wiki 页面而不是我突出显示的部分。
private Cache<Object, String> cache = CacheBuilder.newBuilder()
.concurrencyLevel(4)
.expireAfterAccess(24, TimeUnit.HOURS)
.initialCapacity(1024)
.maximumSize(1_00_000)
.build();
private ConcurrentMap<Object, String> cacheMap = cache.asMap();
这是我的代码,它很简单,只是创建一个 Guava 地图缓存。 问题是,如果我从这张地图中删除()键,GC 是否能够从这张地图中收回内存?不使用 weakReference(我没有存储嵌套对象,如 Set 或 Map 等...)?
是
来自 Guava's Cache.asMap
javadoc(强调我的):
Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to the map directly affect the cache.
并且来自 Guava's Cache Explained wiki page:
At any time, you may explicitly invalidate cache entries rather than waiting for entries to be evicted.
您可能想要检查整个 wiki 页面而不是我突出显示的部分。