[lworld] RFR: 8286824: [valhalla] WeakHashMap for value classes

Roger Riggs rriggs at openjdk.org
Fri Jul 8 15:56:59 UTC 2022


On Thu, 7 Jul 2022 22:36:02 GMT, Mandy Chung <mchung at openjdk.org> wrote:

>> 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.
>> 
>> 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 WeakReferences.
>
> src/java.base/share/classes/java/util/WeakHashMap.java line 231:
> 
>> 229:      * The default {@code ValuePolicy} is {@link ValuePolicy#SOFT} unless
>> 230:      * the system property {@systemProperty java.util.WeakHashMap.valueKeyRetention}
>> 231:      * is equal to the name of a {@link ValuePolicy} enum.
> 
> Is the value of the system property case-sensitive?  should specify.

Enum names are case-sensitive; I'll expand to refer to `ValuePolicy.valueOf` for the value of the property.

-------------

PR: https://git.openjdk.org/valhalla/pull/718



More information about the valhalla-dev mailing list