Robert Silver
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.SoftReferenc e and
java.lang.ref.WeakReferenc e
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.co m/java-api -examples/ index.php? api=java.l ang.ref.So ftReferenc e
SoftReference-eg.txt
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.SoftReferenc
java.lang.ref.WeakReferenc
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.co
SoftReference-eg.txt
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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???
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.
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