What is a softly reachable object?

杀马特。学长 韩版系。学妹 提交于 2020-01-04 05:50:47

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/51891168/what-is-a-softly-reachable-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!