Link to home
Start Free TrialLog in
Avatar of Robert Silver
Robert SilverFlag for United States of America

asked on

What is the difference between Weakly Reachable and Softly Reachable Objects?

What is the difference between Weakly Reachable and Softly Reachable Objects?
This obscure memory manage  set of classes is fully understandable except for these oddly almost identically sounding reachabilites presumably represented by the Classes java.lang.ref.SoftReference and
java.lang.ref.WeakReference

However  maybe its just me but the following makes little sense to me in determining the differences:
Right from Oracle.com's own website
•      An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference
(My comment added - like the class of SoftReference??

•      An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.

See my dilema as the Softly reachable is defined by not being Weakly reachable and vise versa - A real catch 22 unless you really understand the nuance there.
Here is an example of use of the SoftReference class:
e.g http://www.programcreek.com/java-api-examples/index.php?api=java.lang.ref.SoftReference
SoftReference-eg.txt
Avatar of dpearson
dpearson

My understanding of this is that they are very nearly the same.  That the distinction is how the garbage collector behaves.  If you have only get a WeakReference to an object then it *will* be garbage collected on the next pass.  If you have a SoftReference to the object then it *may* be garbage collected on the next pass.

So you use a WeakReference when you want to get the memory back as soon as nobody else is using it.
You use a SoftReference when you're OK with it staying in memory but can survive if it leaves (a cache is a classic example).

Does that help clarify the difference?

Doug
Avatar of Robert Silver

ASKER

So the real difference comes into play with Caches ? Yes?  basically I could enqueue(Add to the ReferenceQueue and stick my weak and soft references into that queue and the Weak reference will be collected as garbage even though a reference may be in in th ReferenceQueue? But a Soft reference if it has a reference in the ReferenceQueue and in addition a Cache then it will not be collected as garbage?
I may now be even more confused by your answer unless I can put together what truly defines a WeakReference Object and
what defines a SoftReference Object. They could not have been more confusing at Sun/Oracle if they tried.
How would the java.lang.ref classes look disassembled I wonder would that maybe confuse me further?
Perhaps Sun/Oracle gave us the different reference classes to avoid the confusion but I still do not see the capabilities or how they should behave deterministically speaking.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Absolutely!  You are suggesting different degrees of gc collectability(just made that up) so given that scenario you made it clear that the SoftReference object when reachable keeps the WeakReference Object because its the same object  but once the SoftReference has been nulled - System.gc() now has the ability to remove both SoftReference and WeakReference
and again WeakReference objects really have no staying powers - They are super temporary in nature alive (not null)
until the next System.gc() is called  - Yes???
Yes I think you've got it.