I'd like to find out how many bytes are being using in the cache. This is useful in determining reasonable sizing. What are some good ways to tally the number of bytes used in a Google Guava cache?
The stats
method doesn't give what I want; it does not include metrics on the number of bytes in the cache.
The asMap
method is the best way I've found so far. After getting this information, one could use some of the techniques shown in In Java, what is the best way to determine the size of an object?. But, frankly, these seem fairly painful, at least from a Clojure codebase. In order to avoid some dependencies, I'm currently using a rough shortcut with Nippy, a Clojure serialization library: (count (nippy/freeze (.asMap cache)))
. I'm looking for better ways.
I am using Google Guava caching from a Clojure codebase, but my question is not necessarily Clojure specific; Java interop is relatively easy in most cases.
Update: Some context in response to a comment below. First, I'd like to know if I'm overlooking a useful part of the Google Guava caching API. Second, I don't know if the generic approaches I linked (for counting memory usage on the heap) apply well to Guava. More broadly, finding cache size utilization is an important use case, so I'm a little surprised it isn't better documented online.