android:软引用/弱引用示例
我的应用程序收到 OutOfMemoryError
错误。当我浏览一些教程时,我开始知道,我可以使用Softreference/Weakreference
来解决这个问题。但我不知道如何使用Softreference/Weakreference
。
请向我推荐一些提供软引用或弱引用示例的教程。
谢谢...
I am getting OutOfMemoryError
on my application. When i went through some tutorials, i came to know that, I can solve this issue by using Softreference/Weakreference
. But I don't know that how to use Softreference/Weakreference
.
Please suggest me some tutorials that providing examples for the Softreference or Weakreference.
Thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅以下教程
如何使用 SoftReference
See the following tutorial
How to use SoftReference
要创建
WeakReference
,语法为WeakReferencemyWeakReference = new WeakReference(actualObject);
。要通过该WeakReference
检索对象,请执行检查if (weakWidget == null)
。这样,如果已经被垃圾收集,您将避免 NullPointerException。Ethan Nicholas 撰写的这篇 Java.net 文章 解释了为什么你想要使用
WeakReference
而不是强引用。它提供了名为Widget
的final
(不可扩展)类的示例,该类没有定义串行 UID,假设开发人员决定定义一个串行 UID 来跟踪每个小部件
实例。他们通过创建一个新的HashMap
并执行诸如serialNumberMap.put(widget, widgetSerialNumber);
之类的操作来实现这一点,这是一个强引用。这意味着当不再需要它时必须明确清理它。开发人员有责任确切地知道何时手动“垃圾收集”该引用并将其从HashMap
中删除,只有当他们确实确定它是时才应该这样做不再需要了。这可能是您在应用程序中遇到的问题。在这种特殊情况下,正如文章所解释的,开发人员可以改用 WeakHashMap 类(如 @NayAneshGupte 在他的示例中所说),其中键实际上是 WeakReference。这将允许 JVM 在其认为合适的情况下取消旧 Widget 的键,以便垃圾收集器可以出现并销毁其关联的对象。
文章还继续讨论了 SoftReferences 和 PhantomReferences(我从未使用过)。您可以在 这篇 javapapers.com 文章 和 < a href="http://www.rallydev.com/community/engineering/java-references-strong-soft-weak-phantom" rel="nofollow">此 Rally 博客。
To create a
WeakReference
, the syntax isWeakReference<SomeType> myWeakReference = new WeakReference<SomeType>(actualObject);
. To retrieve the object via thatWeakReference
, do the checkif (weakWidget == null)
. That way, you will avoid aNullPointerException
if it has been garbage-collected already.This Java.net article by Ethan Nicholas explains why you would want to use a
WeakReference
instead of a strong one. It provides the example of afinal
(unextendible) class calledWidget
that has no defined serial UID, presuming that the developer decides to define a serial UID to track eachWidget
instance. They do so by creating a newHashMap
and doing something likeserialNumberMap.put(widget, widgetSerialNumber);
which is a strong reference. That means it must be explicitly cleaned up when no longer needed. The developer is responsible for knowing exactly when to manually "garbage-collect" that reference and remove it from theHashMap
, which should be done only when they're really sure it's not needed anymore. This may be the problem you ran into in your application.In this particular case, as the article explains, the developer could use the
WeakHashMap
class instead (as @NayAneshGupte put in his example), wherein the key is actually aWeakReference
. This would allow the JVM nullify the keys to oldWidget
s as it saw fit, so that the garbage collector could come along and destroy their associated objects.The article also goes on to talk about
SoftReferences
andPhantomReferences
(which I've never used). You can read more about all of these in this javapapers.com article and this Rally blog.