I am implementing a simple cache using LinkedHashMap
based on the instructions found here. I use the following code:
public class Cache extends LinkedHashMap {
private final int capacity;
public Cache(int capacity) {
super(capacity + 1, 1.1f, true);
this.capacity = capacity;
}
protected boolean removeEldestEntry(Entry eldest) {
return size() > capacity;
}
}
This is very easy. However, it simply imposes a fixed size on the map. I am running on a very small heap and depending on the size of the cached objects and my chosen capacity this could still run out of memory. The objects are arbitrary and so I can't estimate how big they might be. I don't want to depend on SoftReferences
to prune the cache because the way those are cleaned up is unreliable; it changes from VM to VM, and they might either get reclaimed too soon, or they might never get reclaimed until they fill up my heap.
Is there any way for me to monitor the size of the map and constrain based on that?