-
Bug
-
Resolution: Fixed
-
P3
-
repo-valhalla
WeakHashMap uses WeakReference for keys, allowing it to retain only keys and values that are strongly referenced elsewhere.
The behavior of WeakReference (or any Reference class) is not defined for value objects.
WeakHashMap could benefit from one or modes to determine how value objects as keys should be allowed or retained.
Four (4) behaviors are proposed for WeakHashMap to give the developer a choice:
- SOFT - Keys that are value objects are retained until memory pressure clears soft references; then the keys that are value objects are removed automatically
- STRONG - Keys are value objects are retained until they are explicitly removed
- THROW - Keys that are value objects can not be put into the map; UnsupportedOperationException is thrown
- DISCARD - Keys that are value objects are silently discarded; as if they were removed immediately without being visible
The default is SOFT - key/values are removed when there is memory pressure.
Entries will be removed automatically at some point, though less deterministically than with Weak references.
The behavior of WeakReference (or any Reference class) is not defined for value objects.
WeakHashMap could benefit from one or modes to determine how value objects as keys should be allowed or retained.
Four (4) behaviors are proposed for WeakHashMap to give the developer a choice:
- SOFT - Keys that are value objects are retained until memory pressure clears soft references; then the keys that are value objects are removed automatically
- STRONG - Keys are value objects are retained until they are explicitly removed
- THROW - Keys that are value objects can not be put into the map; UnsupportedOperationException is thrown
- DISCARD - Keys that are value objects are silently discarded; as if they were removed immediately without being visible
The default is SOFT - key/values are removed when there is memory pressure.
Entries will be removed automatically at some point, though less deterministically than with Weak references.
- relates to
-
JDK-8319201 [lworld] WeakValuePolicyTest fails with AssertionError: map.containsKey after GC
- Open
-
JDK-8244968 [valhalla] WeakHashMap handling of value object as keys
- Open