Skip to content
This repository has been archived by the owner. It is now read-only.
This repository has been archived by the owner. It is now read-only.

memorySize caculate wrong when put #3

Description

@adam0806

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions