Search code examples
javagarbage-collectionsoft-references

What is a softly reachable object?


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?


Solution

  • Strong Reference , Soft Reference and Weak Reference.

    Student strongReference = new Student(); 
    
    WeakReference<Student> weakReference = new WeakReference<>(strongReference);
    

    Similarly

    Student strongRef = new Student();
    SoftReference<Student> softReference = new SoftReference<>(strongRef);
    

    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

    1. An object which has No Strong Reference and has only Soft or Weak Reference is Softly Reachable

    2. An object having only WeakReference and no Strong or soft references is Weekly Reachable

    3. 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.

    Student stRef = new Student();
    SoftReference <Student> sfRef = new SoftReference<>(stRef);
    stRef = null;
    

    Or

    SoftReference <Student> sfRef = new SoftReference<>(new Student());
    

    To use the object get() method is used but ve aware that it gives you a strong Reference.

    Suppose you have

    Student strongReference = new Student(); 
    
    SoftReference<Student> softReference = new SoftReference<>(strongReference);
     strongReference = null; // object in heap is softly reachable now
     Student anotherStrongReference = softReference.get();
     if(anotherStrongReference != null){
          // you have a strong reference again
     }
    

    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 use get() 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 type WeakReference.java and an object in Heap of type Student is weekly reachable. That object can be accessed by weakRefOfStudent.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.