WeakReference<>在这种特定情况下对垃圾回收是否有帮助?

huangapple go评论71阅读模式
英文:

Does WeakReference<> helps with GC on this specific scenario?

问题

我不确定这是否会在长期运行中导致一些内存泄漏问题,我想知道在这种情况下使用弱引用是否有任何区别:

public abstract class ChangeObserver<X extends SomeClass> {

   X someClass;

   public ChangeObserver(X someClass) {
       this.someClass = someClass;
   }

   public void notifySomeClass() {
       onChanged().onChanged(someClass);
   }

   public abstract OnDataChanged<X> onChanged();

   @FunctionalInterface
   public interface OnDataChanged<X> {
       void onChanged(X x);
   }
}

这会在程序的生命周期中引起问题吗?仅通过使用弱引用是否可以解决?

感谢 @Hulk 和 @tashkhisi,我已经得出了这个解决方案,如果有什么遗漏,请随时指正。

public abstract class ChangeObserver<X extends SomeClass> {

    private WeakReference<X> someClassWeakRef;
    private OnDataChanged<X> observer;

    public ChangeObserver(X someClass) {
        this.someClassWeakRef = new WeakReference<>(someClass);
        observer = onChanged();
    }

    public void notifySomeClass() {
        observer.onChanged(someClassWeakRef.get());
    }

    public abstract OnDataChanged<X> onChanged();

    @FunctionalInterface
    public interface OnDataChanged<X> {
        void onChanged(X x);
    }
}

我仍然不清楚 removeObserver() 方法应该如何执行来移除观察者,如果我要使用该特定方法来避免内存泄漏,是否只需将 observer 设置为 null?

提前感谢。

英文:

I'm not sure if this will give me some memory leak problems in the long run, and I'm wondering if it makes any difference to use a weakreference in this scenario:

public abstract class ChangeObserver&lt;X extends SomeClass&gt; {

   X someClass;

   public ChangeObserver(X someClass) {
       this.someClass= someClass;
   }

   public void notifySomeClass(
   ) {
       onChanged().onChanged(someClass);
   }

   public abstract OnDataChanged&lt;X&gt; onChanged();

   @FunctionalInterface
   public interface OnDataChanged&lt;X&gt; {
       void onChanged(X x);
   }

}

will this cause problems in the program's lifecycle? would it get resolved simply by using a weakreference?

Thanks to @Hulk and @tashkhisi I've come with this solution, If, there is something missing, please don't hesitate on correcting it.

public abstract class ChangeObserver&lt;X extends SomeClass&gt; {

    private WeakReference&lt;X&gt; someClassWeakRef;
    private OnDataChanged&lt;X&gt; observer;

    public ChangeObserver(X someClass) {
        this.someClassWeakRef = new WeakReference&lt;&gt;(someClass);
        observer = onChanged();
    }

    public void notifySomeClass(
    ) {
        observer.onChanged(someClassWeakRef.get());
    }

    public abstract OnDataChanged&lt;X&gt; onChanged();

    @FunctionalInterface
    public interface OnDataChanged&lt;X&gt; {
        void onChanged(X x);
    }

}

What I'm still missing is what exactly does this removeObserver() method should do to remove the observer, If I were to use that specific method of avoiding memory leaks, is it just simply setting the observer = null ?

Thanks in advance.

答案1

得分: 2

是的,在Java中解决“Observer”模式中的内存泄漏问题有很多方法之一是向您的类中添加另一个方法,例如“removeObserver”,用于从“Observable”中移除不再需要的Observer对象,第二种方法是使用弱引用,如下所示:(我更愿意将其命名为“someObject”,而不是“someClass”,但我遵守了您的命名约定)

WeakReference<X> x = new WeakReference<X>(someClass);
英文:

Yes There are many ways of tackling memory leak in Observer Pattern in Java one of them is to add another method like removeObserver to your class for removing Observer object from Observable when you don't need them any more and the second way is to use Weak reference like below:( I would rather named it someObject instead of someClass but I have obeyed your naming convention)

WeakReference&lt;X&gt; x = new WeakReference&lt;X&gt;(someClass); 

huangapple
  • 本文由 发表于 2020年8月31日 22:41:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63672941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定