LruCache
LruCache copied to clipboard
memorySize caculate wrong when put
When put item more then map capacity, we should reduce the top item valuesSize first.
This is what I do.
@Override public V put(K key, V value) { Objects.requireNonNull(key, "key == null"); Objects.requireNonNull(value, "value == null"); V previous; synchronized (this){ if(map.size() + 1 > capacity){ Map.Entry<K, V> toRemove = map.entrySet().iterator().next(); memorySize -= getValueSize(toRemove.getValue()); } previous = map.put(key, value); memorySize += getValueSize(value); if(previous != null){ memorySize -= getValueSize(previous); } trimToSize(maxMemorySize); } return previous; }