1

guava 缓存可以使用的最大优选大小是多少?我的系统有 8 GB RAM,所以在不降低系统和其他应用程序性能的情况下,我可以使用多少 MB?当 RAM 存储不可用时,Guava 缓存是否也使用硬盘存储?

4

1 回答 1

6

You can only limit the number of entries in the cache. In Guava there is no way to specify the size limit in megabytes. EHCache supports specifying the size limit in megabytes, however, determining the size of a cached entry, is a difficult task and involves some magic, guessing, and, a performance overhead.

Does Guava cache also use hardDisk storage when RAM storage is not available?

No, it is Java heap only. See CachesExplained · google/guava Wiki for details.

In practice you start with a reasonable but low cache size, and increase it, while keeping and eye on your heap memory consumption.

Actually the question "How to size a cache?" is not easy to answer, usually there is experience combined with gut feeling involved. To broaden the answer a little and give you more directions:

Prerequisites

  • Make sure you understand the general concepts of the Java heap and garbage collection
  • Know/check the maximum heap size setting of your JVM. See: How is the default java heap size determined?
  • Learn how to monitor and interpret the heap consumption and GC activity, e.g. with VisualVM, see: http://visualvm.java.net/monitor_tab.html
  • Know how to retrieve and interpret statistics from the cache, especially the hit rate
  • Know your data, the estimated size in byte of one entry and the size count of your active data set

Rules of thumb

  • Aim for a hit rate of 90% and more. If it is less, increase the cache size, or, have an idea why the cache is not very effective. Possible causes: Cache misses because of a low expiry time, lots of sequential scans. Sometimes you can improve your application or fiddle with other parameters then the cache size to optimize your hit rate.
  • For a first "best bet" configure your cache size (in entry counts) at least of 10% of your active data set size
于 2015-12-28T09:58:25.893 回答