LruCache icon indicating copy to clipboard operation
LruCache copied to clipboard

memorySize caculate wrong when put

Open adam0806 opened this issue 6 years ago • 0 comments

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; }

adam0806 avatar Oct 12 '19 01:10 adam0806