K - the type of keys maintained by this mapV - the type of mapped valuespublic class SoftHashMap<K,V> extends Object implements Map<K,V>
SoftHashMap that stores its values in SoftReferences.
N.B. JDK's WeakHashMap, which uses WeakReferences for
its keys rather than for its values. See SoftKeyHashMap
for a WeakHashMap-similar implementation using SoftReference-ed keys.
Having the values wrapped by soft references allows the cache to automatically reduce its size based on memory limitations and garbage collection. This ensures that the cache will not cause memory leaks by holding strong references to all of its values.
This implementation is thread-safe and usable in concurrent environments.
SoftKeyHashMap| Constructor and Description |
|---|
SoftHashMap()
Creates a new SoftHashMap with a default retention size size of
DEFAULT_RETENTION_SIZE (100 entries). |
SoftHashMap(int retentionSize)
Creates a new SoftHashMap with the specified retention size.
|
SoftHashMap(Map<K,V> source)
Creates a
SoftHashMap backed by the specified source, with a default retention
size of DEFAULT_RETENTION_SIZE (100 entries). |
SoftHashMap(Map<K,V> source,
int retentionSize)
Creates a
SoftHashMap backed by the specified source, with the specified retention size. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<K,V>> |
entrySet() |
V |
get(Object key) |
boolean |
isEmpty() |
Set<K> |
keySet() |
V |
put(K key,
V value)
Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection.
|
void |
putAll(Map<? extends K,? extends V> m) |
V |
remove(Object key) |
int |
size() |
Collection<V> |
values() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAllpublic SoftHashMap()
DEFAULT_RETENTION_SIZE (100 entries).SoftHashMap(int)public SoftHashMap(int retentionSize)
The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (ie 'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references.
Note that in a highly concurrent environments the exact total number of strong references may differ slightly
than the actual retentionSize value. This number is intended to be a best-effort retention low
water mark.
retentionSize - the total number of most recent entries in the map that will be strongly referenced
(retained), preventing them from being eagerly garbage collected by the JVM.public SoftHashMap(Map<K,V> source)
SoftHashMap backed by the specified source, with a default retention
size of DEFAULT_RETENTION_SIZE (100 entries).source - the backing map to populate this SoftHashMapSoftHashMap(Map,int)public SoftHashMap(Map<K,V> source, int retentionSize)
SoftHashMap backed by the specified source, with the specified retention size.
The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (ie 'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references.
Note that in a highly concurrent environments the exact total number of strong references may differ slightly
than the actual retentionSize value. This number is intended to be a best-effort retention low
water mark.
source - the backing map to populate this SoftHashMapretentionSize - the total number of most recent entries in the map that will be strongly referenced
(retained), preventing them from being eagerly garbage collected by the JVM.public boolean containsKey(Object key)
containsKey in interface Map<K,V>public boolean containsValue(Object value)
containsValue in interface Map<K,V>public V put(K key, V value)
put in interface Map<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keykey, or
null if there was no mapping for key.
(A null return can also indicate that the map
previously associated null with key,
if the implementation supports null values.)Copyright © 2020 GSI Helmholtzzentrum für Schwerionenforschung GmbH. All rights reserved.