当前位置: 首页 > 工具软件 > disposable > 使用案例 >

RxJava重复创建Disposable导致在销毁的时候销毁不全面,出现内存泄漏

林星阑
2023-12-01

泄漏的引用栈:

In com.eebbk.askhomework.content:1.10.0.0:1100000.
bfc-leakcanary:5.0.12-bugfix.
* com.eebbk.askhomework.content.view.MainActivity has leaked:
* GC ROOT com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.contextClassLoader (anonymous subclass of java.lang.Thread)
* references dalvik.system.PathClassLoader.runtimeInternalObjects
* references array java.lang.Object[].[749]
* references static com.eebbk.rxbus.RxBus.INSTANCE
* references com.eebbk.rxbus.RxBus.bus
* references com.jakewharton.rxrelay2.SerializedRelay.actual
* references com.jakewharton.rxrelay2.PublishRelay.subscribers
* references java.util.concurrent.atomic.AtomicReference.value
* references array com.jakewharton.rxrelay2.PublishRelay$PublishDisposable[].[0]
* references com.jakewharton.rxrelay2.PublishRelay$PublishDisposable.downstream
* references io.reactivex.internal.operators.observable.ObservableFilter$FilterObserver.downstream
* references io.reactivex.internal.operators.observable.ObservableMap$MapObserver.downstream
* references io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.downstream
* references io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.downstream
* references io.reactivex.internal.observers.LambdaObserver.onNext
* references com.eebbk.askhomework.content.other.moduleother.widget.search.-$$Lambda$SearchPoetryView$9OEbyBPXjurffCFOu0WvAH_oVN8.f$0
* references com.eebbk.askhomework.content.other.moduleother.widget.search.SearchPoetryView.mContext
* leaks com.eebbk.askhomework.content.view.MainActivity instance

 

问题的原因是我对Disposable对象实现了多次赋值,而只销毁一次

在类创建的时候调用

private void initRxBus() {
    mPoemStopAnimDisposable = RxBus.getInstance().toSubscriber(PoemCardViewStopAnim.class)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(o -> {
                if (mPoemView == null){
                    return;
                }
                mPoemView.resetAllPlayStatus();
                mPoemView.stopAuthor();
                mPoemView.stopTitle(false);
                isPlayTitle = false;
                curPlayIndex = -3;
                mPauseVoice.clear();
                mCurrentVoices.clear();
            });
}

 

为了防止内存泄漏(错误操作)

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if (mPoemStopAnimDisposable != null) {
        mPoemStopAnimDisposable.dispose();
        mPoemStopAnimDisposable = null;
    }
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    initRxBus();
}

 

这样比如这个类创建了执行了一次initRxBus ,在onAttachedToWindow再次执行initRxBus,而在onDetachedFromWindowd中ispose一次,导致漏了一次dispose

解决方法是在创建的时候判断一下。

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    if (mPoemStopAnimDisposable == null || mPoemStopAnimDisposable.isDisposed()) {
        initRxBus();
    }
}
}

 

 类似资料: