RxAndroid - 修复活动泄漏?

时间:2016-01-14 00:01:40

标签: android performance memory-leaks rx-java rx-android

我已将事情简化为下面显示的Activity,其中包含相关的build.config和activity_main.xml。

问题是,如果你在计数器完成之前按BACK,LeakCanary会报告Activity的泄漏,而让它运行到最后则不会。

我知道发生了什么。我知道必须在Observer中创建一个匿名内部类,它在onPause()调用中幸存下来,因为如果你观察控制台,你可以看到它继续计数。我知道我已经取消订阅了,但是这个过程中的线程仍然作为Activity的内部类运行,这是泄漏的标志。对于我来说,这不仅仅是一个人为的角落情况,在我的真实应用程序中,计数应该在方向改变后继续,所以我坚持保留片段中的必要条件并相应地获取计数,具有相同的泄漏。我只是不需要在这里展示它来简化示例。

MainAcivity.java:

package com.otamate.rxactivityleaker;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.squareup.leakcanary.LeakCanary;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private Observable<Long> mObservable;
    private Subscriber<Long> mSubscriber;

    public TextView mTextView;
    public final static int MAX_PROGRESS = 10;
    public final static int EMIT_DELAY_MS = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LeakCanary.install(getApplication());

        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.textView);

        mSubscriber = createSubscriber();
        mObservable = createObservable();

        mObservable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(mSubscriber);
    }

    private Observable createObservable() {
        return Observable.create (
            new Observable.OnSubscribe<Long>() {

                @Override
                public void call(Subscriber<? super Long> sub) {
                    for (long i = 1; i < MAX_PROGRESS + 1; i++) {

                        Log.d("Observable", "Progress: " + i);

                        sub.onNext(i);
                        SystemClock.sleep(EMIT_DELAY_MS);
                    }
                    sub.onCompleted();
                }
            }
        );
    }

    private Subscriber createSubscriber() {
        return new Subscriber<Long>() {

            @Override
            public void onNext(Long val) {
                Log.d("Subscriber", "Loop " + val);

                mTextView.setText("Progress: " + val);
            }

            @Override
            public void onCompleted() {
                Log.d("Subscriber", "Completed");

                mTextView.setText("Done!");
            }

            @Override
            public void onError(Throwable e) {
                Log.d("Subscriber", "Error: " + e);

                mTextView.setText("Error!");
            }
        };
    }

    @Override
    public void onPause() {
        super.onPause();

        if (mSubscriber != null) {
            Log.d("MainActivity", "onPause() Unsubscribed");

            mSubscriber.unsubscribe();
        }
    }
}

这是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.otamate.rxactivityleaker.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Idle" />
</RelativeLayout>

的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.otamate.rxactivityleaker"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'io.reactivex:rxandroid:0.25.0'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}

2 个答案:

答案 0 :(得分:0)

Observable.create将其easy for you to shoot yourself in the foot

你的问题在这里:

private Observable createObservable() {
    return Observable.create (
        new Observable.OnSubscribe<Long>() {

            @Override
            public void call(Subscriber<? super Long> sub) {
                for (long i = 1; i < MAX_PROGRESS + 1; i++) {

                    Log.d("Observable", "Progress: " + i);

                    sub.onNext(i);
                    SystemClock.sleep(EMIT_DELAY_MS);
                }
                sub.onCompleted();
            }
        }
    );
}

在创建Observable时,您不会在调用Subscriber之前检查onNext/onCompleted是否取消订阅。你也没有处理背压。

结帐AbstractOnSubscribe它可以帮助您更轻松地创建Observable

编辑:AbstractOnSubscribe已在1.1中删除,但SyncOnSubscribe可能会有效。

答案 1 :(得分:0)

试试这个

Subscription subscription;
@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LeakCanary.install(getApplication()); 

    setContentView(R.layout.activity_main);

    mTextView = (TextView) findViewById(R.id.textView);

    mSubscriber = createSubscriber();
    mObservable = createObservable(); 

    subscription = mObservable.subscribeOn(Schedulers.io()) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(mSubscriber);
} 

比onPause()

@Override 
public void onPause() { 
    super.onPause(); 

    if (subscription != null) {

        subscription.unsubscribe();
    } 
}