I am trying to study the meaning of a soft reference via this 'Soft References in Java' article:
https://www.baeldung.com/java-soft-references
My problem in understanding this article is that it defines the term "soft reference" via the term "softly reachable object", but I don't know what a "softly reachable object" means.
That is, an object in the heap either has a reference to it or doesn't, right?
A reference either points to a valid object or is null, right?
When does an object become "softly reachable"?
or did I get it all wrong?
Strong Reference , Soft Reference and Weak Reference.
Similarly
During garbage collection if an object in heap has a strong reference to it then it survives, if it does not has strong reference but has WeakReference then it won't survive. It is used to avoid leak when objects are passed out side the life cycle manager context.
SoftReference are like weakreference but they survive garbage collection cycle till memory is available in plenty.
An object is softly reachable if there are no Strong references and have SoftReferences. As an object having only weak reference is eligible for garbage collection and on the other hand an object having only soft reference is more egar to survive garbage collection (as compared to weak reference) hence
An object which has No Strong Reference and has only Soft or Weak Reference is Softly Reachable
An object having only WeakReference and no Strong or soft references is Weekly Reachable
An object with atleast one Strong reference with or without any soft or weak references is Strongly Reachable.
Both the below cases The object in heap is softly reachable.
Or
To use the object
get()
method is used but ve aware that it gives you a strong Reference.Suppose you have
So avoid assiging the non null object returned from
get()
method of Weak or Soft references to static or instance variables else it just defeats the usuage of either of these. Of using any of these then best way store static or instance variable if needed in form of Weak or Soft reference. When ever you need useget()
check for not null and use as Local varaible only. pass to other methods if possible only weak or soft reference.Difference between WeakReference and SoftReference are well explained in various links, one such link is : https://stackoverflow.com/a/299702/504133
P.S. References of type WeakReference and SoftReference objects are strong referenced, it is the wrapped object that is weakly or softly reachable, in case no strong reference are available (The object can be retrieved by using
get()
).WeakReference <Student> weakRefOfStudent = new WeakReference<>(new Student());
weakRefOfStudent
is a strong reference of typeWeakReference.java
and an object in Heap of type Student is weekly reachable. That object can be accessed byweakRefOfStudent.get()
. which may or may not be null if it has been garbage collected or not.This is just to clarify the doubts that may occur.
I think the linked article explains this quite well in the first sentences. Soft reference and softly reachable object are used synonymously there.
So, the correct answer to your question would be, "A softly reachable object is an object which is only referenced by soft references".
About the nature of soft references, well, the article explains this better than I could in some paragraphs here.