Giter Site home page Giter Site logo

LiveDataBus中的bus这个map对象数据是否会一直持有,很多的时候是不是一种内存泄露? about liveeventbus HOT 10 CLOSED

jeremyliao avatar jeremyliao commented on May 17, 2024
LiveDataBus中的bus这个map对象数据是否会一直持有,很多的时候是不是一种内存泄露?

from liveeventbus.

Comments (10)

KittenBall avatar KittenBall commented on May 17, 2024 2

我刚试了下,重写removeObserver可以解决这个问题,虽然看起来好像简单粗暴哈哈

 @Override
    public void removeObserver(@NonNull Observer<T> observer) {
      super.removeObserver(observer);
      if (!hasObservers()) {
        LiveEventBus.bus.remove(eventKey);
      }
    }

from liveeventbus.

KittenBall avatar KittenBall commented on May 17, 2024 1

能否这样,在BusMutableLiveData内重写onInactive方法

protected void onInactive() {
      super.onInactive();
      if (!hasObservers()) {
        LiveEventBus .bus.remove(eventKey);
      }
    }

当然,需要为BusMutableLiveData添加构造方法

public BusMutableLiveData(String eventKey){
     this.eventKey = eventKey;
}

from liveeventbus.

JeremyLiao avatar JeremyLiao commented on May 17, 2024

这个map对象是一直持有的,据我所知,所有的bus类组件,如rxbus,eventbus都会一直持有,但是区别于缓存类组件,bus类组件一般持有的都是String,Integer等小对象,不会持有Bitmap等会占用大量内存的对象,所以不用太担心这个问题

from liveeventbus.

SoarY avatar SoarY commented on May 17, 2024

map集合里面也存了很多LiveData对象,也就是说项目了注册了多少,里面就存多少,没移除操作,这样是否友好,多了以后会不会影响其内存。

from liveeventbus.

JeremyLiao avatar JeremyLiao commented on May 17, 2024

谢谢你的建议,以后会考虑添加把没有订阅者的observable定期清理掉的功能

from liveeventbus.

JeremyLiao avatar JeremyLiao commented on May 17, 2024

嗯,我也是这样想的,在取消订阅或者类似onInactive的时机去检查当前Observer上面是否hasObservers,然后把这个Observer remove掉。这是个很好的思路,谢谢

from liveeventbus.

JeremyLiao avatar JeremyLiao commented on May 17, 2024

能否这样,在BusMutableLiveData内重写onInactive方法

protected void onInactive() {
      super.onInactive();
      if (!hasObservers()) {
        LiveEventBus .bus.remove(eventKey);
      }
    }

当然,需要为BusMutableLiveData添加构造方法

public BusMutableLiveData(String eventKey){
     this.eventKey = eventKey;
}

这种方式确实不错,我再想想有没有什么问题,赞一个

from liveeventbus.

KittenBall avatar KittenBall commented on May 17, 2024

这个方法刚刚验证了一下,是有问题的
onInactive回调时一般都仍然会有一个Observer,翻看源码发现

@Override
        public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
            if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
                removeObserver(mObserver);
                return;
            }
            activeStateChanged(shouldBeActive());
        }

在生命周期变化后,会回调onStateChanged方法,这里如果不为destroy,调用activeStateChanged方法

void activeStateChanged(boolean newActive) {
            if (newActive == mActive) {
                return;
            }
            // immediately set active state, so we'd never dispatch anything to inactive
            // owner
            mActive = newActive;
            boolean wasInactive = LiveData.this.mActiveCount == 0;
            LiveData.this.mActiveCount += mActive ? 1 : -1;
            if (wasInactive && mActive) {
                onActive();
            }
            if (LiveData.this.mActiveCount == 0 && !mActive) {
                onInactive();
            }
            if (mActive) {
                dispatchingValue(this);
            }
        }

在activeStateChanged方法中 只有原先的mActiviCount=0(即1变为0)且mActive为true时才回调onInactive
然而,activity在destroy之前会走onPause,此时就会走上面流程,并回调onInactive,所以这里调用hasObservers返回true

不好意思,之前的方案是错误的

不过应该有方案解决,我看看

from liveeventbus.

JeremyLiao avatar JeremyLiao commented on May 17, 2024

我刚试了下,重写removeObserver可以解决这个问题,虽然看起来好像简单粗暴哈哈

 @Override
    public void removeObserver(@NonNull Observer<T> observer) {
      super.removeObserver(observer);
      if (!hasObservers()) {
        LiveEventBus.bus.remove(eventKey);
      }
    }

我看了LiveData的源码,确实这样是可行的。我准备使用你这种方法来弄。再次表示感谢:)

from liveeventbus.

JeremyLiao avatar JeremyLiao commented on May 17, 2024

这个问题已解决,感谢提出这个问题的小伙伴以及@GreenhairTurtle的解决方案,具体参见commit:9ae7b8c

from liveeventbus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.