You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Jack, Paul" <pj...@sfaf.org> on 2002/07/15 20:16:48 UTC
[collections] SoftRefHashMap (was RE: Avalon excalibur collection
s migration status)
> > * Do something with SoftRefHashMap. Its current form is "all
> > kinds of wonky". All of its little strangenesses should be
> > documented (ie, the containsKey(Object) and containsValue(Object)
> > methods might disagree for a key/value pair). Frankly I'd
> > deprecate the class and provide a more efficient version (one
> > that uses a ReferenceQueue) and a more well-defined version (one
> > that behaves much like a WeakHashMap).
>
> Does the version you wrote and submitted do these things (I think you
> called it ReferenceMap)
Yes. However, now that I think about it, ReferenceMap isn't nearly as
useful as I thought.
The idea was to have one class that allows you to specify soft, weak,
or phantom references for either keys or values or both. However:
1. PhantomReference.get() always returns null. This means that a map
with phantom keys or phantom values is more or less useless. (The keys
or values would disappear as soon as you put them in the map! For instance
for phantom keys, map.put("key", "value") followed by map.containsKey("key")
would always return false.)
2. A map with soft keys would do the same thing as a map with soft
values: When memory is running low, the garbage collector would remove
mappings from the map. So it doesn't seem to be useful to allow people
to specify both soft keys and soft values, since they do the same thing.
3. A map with weak keys would be very useful, but Java already provides
this (java.util.WeakHashMap).
Because of the above three points, the only really useful things that
ReferenceMap provides are:
a. A map with weak values. (Eg, a map whose mappings are automatically
removed if the *value* is garbage collected).
b. A map with soft values. (Eg, a map whose mappings are automatically
removed if memory is running low).
Bear with me. Earlier, Stephen Colbourne suggested that ReferenceMap be
rewritten to be a decorator, so that people could, for instance, have a
softly valued SequencedHashMap. I replied that unfortunately, in order
to be efficient for use with keys, you basically have to have access to the
internal structures of the map.
THEREFORE.
Since phantom keys are useless...
Since soft keys do the same thing as soft values...
Since weak keys are already provided...
Since ReferenceMap was written not as a decorator to provide
referenced keys...
I propose to ignore ReferenceMap, and instead provide this API:
Map MapUtils.softValuedMap(Map map);
Map MapUtils.weakValuedMap(Map map);
SoftRefHashMap could be deprecated in favor of softValuedMap(Map).
Let me know what you think and I'll code it up.
-Paul
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [collections] SoftRefHashMap (was RE: Avalon excalibur collections migration status)
Posted by Juozas Baliuka <ba...@centras.lt>.
It is possible to find a lot of patches in archyve for this class, some I
posted myself a half year ago.
<snip>
> 1. PhantomReference.get() always returns null. This means that a map
> with phantom keys or phantom values is more or less useless. (The keys
> or values would disappear as soon as you put them in the map! For
instance
> for phantom keys, map.put("key", "value") followed by
map.containsKey("key")
> would always return false.)
It useless if used without ReferenceQueue.
Documentation :
"Soft and weak references are automatically cleared by the collector before
being added to the queues with which they are registered, if any. Therefore
soft and weak references need not be registered with a queue in order to be
useful, while phantom references do. An object that is reachable via phantom
references will remain so until all such references are cleared or
themselves become unreachable.
"
>
> 2. A map with soft keys would do the same thing as a map with soft
> values: When memory is running low, the garbage collector would remove
> mappings from the map. So it doesn't seem to be useful to allow people
> to specify both soft keys and soft values, since they do the same thing.
It is not the same. Garbage collector would not remove mapings if they are
reacheble.
Keys can be unreacheble and values can be reacheble at the same time.
SoftValueMap's
are used for cache.
>
> 3. A map with weak keys would be very useful, but Java already provides
> this (java.util.WeakHashMap).
Agree.
>
> Because of the above three points, the only really useful things that
> ReferenceMap provides are:
>
> a. A map with weak values. (Eg, a map whose mappings are automatically
> removed if the *value* is garbage collected).
>
> b. A map with soft values. (Eg, a map whose mappings are automatically
> removed if memory is running low).
>
> Bear with me. Earlier, Stephen Colbourne suggested that ReferenceMap be
> rewritten to be a decorator, so that people could, for instance, have a
> softly valued SequencedHashMap. I replied that unfortunately, in order
> to be efficient for use with keys, you basically have to have access to
the
> internal structures of the map.
>
> THEREFORE.
>
> Since phantom keys are useless...
> Since soft keys do the same thing as soft values...
> Since weak keys are already provided...
> Since ReferenceMap was written not as a decorator to provide
> referenced keys...
>
> I propose to ignore ReferenceMap, and instead provide this API:
>
> Map MapUtils.softValuedMap(Map map);
> Map MapUtils.weakValuedMap(Map map);
>
> SoftRefHashMap could be deprecated in favor of softValuedMap(Map).
>
> Let me know what you think and I'll code it up.
>
> -Paul
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>