0

It is possible remove data from cache, when available runtime memory near with overflow? The cache implementetion is below:

Cache<String, Cache<String, List<String>>> cache = CacheBuilder.newBuilder()
            .maximumWeight((int) ((Runtime.getRuntime().totalMemory() / 1024) * 0.9))
            .weigher((Weigher<String, Cache<String, List<String>>>) (key, val) -> runtimeMemory())
            .build();

public static int runtimeMemory() {
        long totalMemory = Runtime.getRuntime().totalMemory() / 1024;
        long freeMemory = Runtime.getRuntime().freeMemory() / 1024;
        return (int) (totalMemory - freeMemory);
    }

It is work or no ? Or weigher summarizes runtimeMemory() for each value?

2 Answers 2

3

There is no good way for Guava's Cache -- or any on-heap cache implementation, honestly -- to do what you ask. Soft references may appear to work, but are much more likely to cause awful behavior -- e.g. your JVM may perform continuous, constant full garbage collections.

The best way to address this issue is really and truly to experiment and find a maximumSize parameter that works for you. Your implementation will not work, either, as it doesn't attempt to weigh one particular entry.

Sign up to request clarification or add additional context in comments.

Comments

0

If your objective is to avoid running out of memory JVM-wide, then the garbage collector is the right guy for the job. I suggest you use soft-values, like so:

Cache<String, Cache<String, List<String>>> cache = CacheBuilder.newBuilder()
        .softValues()
        .build();

This sets up a cache which wraps any value it stores in a SoftReference. Softly-referenced objects will be garbage-collected in a globally least-recently-used manner in response to memory demand and entry invalidated in the cache.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.