commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael A. Smith" <>
Subject RE: [collections] SoftRefHashMap (was RE: Avalon excalibur collec tions migration status)
Date Thu, 18 Jul 2002 01:35:40 GMT
On Wed, 17 Jul 2002, Jack, Paul wrote:
> > Very possible I understand WeakReference wrong, but as I understand GC
> > clears it, and GC runs then memory is low (possible it is not 
> > true), of course reachability is weaker. If it is true, both 
> > reference types are almost the same for cache.
> Ah, yes, you're correct.  If the garbage collector is a "stop-the-world"
> one that only runs when memory is low, then WeakReference would behave
> much like SoftReference.  But, it's not necessarily the case.  The 
> garbage collector can run incrementally, continuously in a background
> thread, in response to memory demand, or even in different ways for
> different threads, depending on the JVM implementation.  The JDK1.4
> VM lets you specify whether the garbage collector is incremental or
> "stop-the-world".  So, in many cases, WeakReference would behave 
> differently from SoftReference.

Ummm..  I'd go so far as to say that in *all* cases, WeakReferences 
would behave differently than SoftReferences.  WeakReferences are 
cleared once an object is only weakly reachable.  SoftReferences are 
*not* cleared when they are only softly reachable -- they are cleared 
when memory constraints demand.  That's two very different behaviors and 
it doesn't matter whether the GC runs incrementally or in a 
stop-the-world manner. 

> We could use an internal SequencedHashMap to store strong references,
> actually, since it only keeps the x most recent mappings.

I think it's unnecessary.  If the JVM needs the memory, it needs the
memory.  The VM should not be clearing SoftReferences if it doesn't need
the memory.  It is true, though, that there are no constraints on
*which* soft references are cleared, but the API spec says that "Virtual
machine implementations are, however, encouraged to bias against
clearing recently-created or recently-used soft references."  Thus, you
already have a SequencedHashMap type thing for those recently
created/used references.  Yeah, ok, recently created isn't as useful as
recently used.  But I think it's better to leave it up to the
application of SoftRefHashMap to hold onto strong references rather than
build it into SoftRefhashMap itself.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message