什么是柔软可触及的物体? [英] What is a softly reachable object?

查看:167
本文介绍了什么是柔软可触及的物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过这篇"Java中的软引用"文章来研究软引用的含义:

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"?

还是我弄错了?

推荐答案

强引用,软引用和弱引用.

Strong Reference , Soft Reference and Weak Reference.

Student strongReference = new Student(); 

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

类似地

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

在垃圾回收期间,如果堆中的对象对其具有强引用,则该对象将保留下来;如果该对象没有强引用但具有WeakReference,则该对象将无法保留.当对象从生命周期管理器上下文中传递出去时,它用于避免泄漏.

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就像弱引用,但是它们可以在垃圾回收周期中生存,直到有足够的内存可用为止.

SoftReference are like weakreference but they survive garbage collection cycle till memory is available in plenty.

如果没有强引用并且具有SoftReferences,则对象是可软到达的.因为只有弱引用的对象才有资格进行垃圾回收,另一方面,只有软引用的对象更容易受到垃圾回收的影响(与弱引用相比)

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. 没有强引用且仅具有软引用或弱引用的对象可以轻易到达

只有WeakReference而没有强引用或软引用的对象是每周可访问的

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.

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

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

要使用对象get()方法,但已经意识到它为您提供了强大的参考.

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

假设您有

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
 }

因此,请避免将从弱引用或软引用的get()方法返回的非null对象辅助静态或实例变量,否则它只会挫败这两种方法的使用.如果需要,以弱引用或软引用的形式使用这些最佳方式存储静态或实例变量.每当需要使用get()时,请检查是否不为null并仅用作本地变量.如果可能,仅传递给弱引用或软引用.

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.

WeakReference和SoftReference之间的区别在各种链接中都有很好的解释,其中一个链接是: https://stackoverflow.com/a/299702/504133

Difference between WeakReference and SoftReference are well explained in various links, one such link is : https://stackoverflow.com/a/299702/504133

P.S.类型为WeakReference和SoftReference对象的引用是强引用,在没有强引用的情况下,它是弱或软可访问的包装对象(可以使用get()检索该对象). WeakReference <Student> weakRefOfStudent = new WeakReference<>(new Student());

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());

weakRefOfStudentWeakReference.java类型的强引用,并且每周可以访问Student类型的Heap中的对象.可以通过weakRefOfStudent.get()访问该对象.如果已被垃圾回收,则为null或不为null.

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.

这篇关于什么是柔软可触及的物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆