【问题标题】:How would an anonymous class get GC'd in picasso on Android?匿名类如何在 Android 上的 picasso 中获得 GC?
【发布时间】:2013-12-10 21:21:03
【问题描述】:

谁能给我解释一下comment here

在调用 Picasso 时不要创建匿名的 Target 类 收集垃圾。保留成员字段作为强引用 防止它被 gc'ed

根据line 30 of ImageViewAction.java,回调是一个强引用。

ImageViewAction(Picasso picasso, ImageView imageView, Request data, boolean skipCache,
      boolean noFade, int errorResId, Drawable errorDrawable, String key, Callback callback) {
    super(picasso, imageView, data, skipCache, noFade, errorResId, errorDrawable, key);
    this.callback = callback;
  }

假设 Callback 是一个匿名类,它将创建对其父类的引用,从而防止父类也被 GC。

根据line 48 of Action.java,目标本身是一个弱引用,但这不是回调。

  Action(Picasso picasso, T target, Request data, boolean skipCache, boolean noFade,
      int errorResId, Drawable errorDrawable, String key) {
    this.picasso = picasso;
    this.data = data;
    this.target = new RequestWeakReference<T>(this, target, picasso.referenceQueue);

谁能解释我的误解?

【问题讨论】:

  • 某人必须持有对您的 Target 的引用,否则它将被 gc'ed 因为毕加索只存储了一个 WeakReference
  • 啊,我明白了 - 您的评论仅适用于目标而不是回调。那么使用匿名回调应该没问题吧?
  • 是的,匿名回调被保留为强参考。强烈建议您调用 cancel(target) 以释放引用并防止在 Picasso 下载完成之前临时泄漏。

标签: android anonymous-inner-class picasso


【解决方案1】:

我很困惑,评论指的是目标(ImageView)而不是回调。使用匿名回调的模式很好。

【讨论】:

    猜你喜欢
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多