Re: more on SoftReference, WeakReference and PhantomReference

Phillip Rhodes <[email protected]>
Newsgroups gmane.org.user-groups.trijug.juglist
Message-ID <[email protected]>
Mark Bennett wrote:

> That's interesting.  I did RTFA but I still don't understand the 
> difference between weak and soft.
> 

Basically it's this: once an object has only "weak" 
references, it is considered "weakly reachable" and will
be garbage collected (more or less) immediately.

OTOH, an object which has one or more "soft references" (but 
no strong references) is considered "softly reachable" and 
will (generally) not be collected until the VM chooses to do 
so **in response to memory conditions.**  This is why you
will hear people say "Use a SoftReference to implement a 
"memory sensitive cache."  If memory starts to run low, the 
VM can start collecting the "softly reachable" objects.

Maybe this from the javadoc will make it a little more clear:

***********************************************************
Soft reference objects, which are cleared at the discretion 
of the garbage collector in response to memory demand. Soft 
references are most often used to implement memory-sensitive 
caches.
<snip>
All soft references to softly-reachable objects are 
guaranteed to have been cleared before the virtual machine 
throws an OutOfMemoryError. Otherwise no constraints are 
placed upon the time at which a soft reference will be 
cleared or the order in which a set of such references to 
different objects will be cleared. Virtual machine 
implementations are, however, encouraged to bias against 
clearing recently-created or recently-used soft references.
<snip>
**********************************************************

So all that's guaranteed is that they will be collected 
before an OOM Error is thrown, but the VM is "encouraged" to 
keep the softly reachable ones around longer.  That's really 
the difference in a nutshell... a softly reachable object 
should, generally speaking, persist in memory longer than a 
weakly reachable one.  So use WeakReference to avoid memory 
leaks caused by storing references in collections, or by
registering event listeners, etc.  Use SoftReference to 
implement a cache that will be cleared dynamically if memory
starts to run low.

In either case, the object *may* be collected by the
garbage collector at some point... so if you call get() on 
the reference object, you have to be prepared for it to
return null, and to recreate the object in question.  But a 
"softly reachable" object is *likely* to remain in memory
longer than a "weakly reachable" one.

Does that help?  If not, you might want to check out the 
sample code from last Monday's Tri-JUG talk, there are a 
couple of little demos that are designed to illustrate the 
difference.  Look at the Reference1 and Reference2 projects.


TTYL,


Phil
-- 
North Carolina - First In Freedom

Free America - Vote Libertarian
www.lp.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.