LeakCanary of Android memory optimization

LeakCanary is an artifact for checking memory leaks

Although I didn't find any memory leaks

Is my code written ok or my opening method is incorrect?

anyway

Check for memory leaks before your app goes live

is necessary work


learn from

https://blog.csdn.net/itachi85/article/details/77826112?utm_source=gold_browser_extension


rely

 dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
 }

Application

public class LeakApplication extends Application {
    private RefWatcher refWatcher;
    @Override
    public void onCreate() {
        super.onCreate();
        refWatcher= setupLeakCanary();
    }
    private RefWatcher setupLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return RefWatcher.DISABLED;
        }
        return LeakCanary.install(this);
    }

    public static RefWatcher getRefWatcher(Context context) {
        LeakApplication leakApplication = (LeakApplication) context.getApplicationContext();
        return leakApplication.refWatcher;
    }
}

detect

RefWatcher refWatcher = LeakApplication.getRefWatcher(this);//1
refWatcher.watch(this);

Generally, this is written in his onDestory method in the activity, and other Java classes can also monitor at any time.


important point

1.The release version is invalid

2. After entering the app, run it for a while. After there is a memory leak, the icon will come out (if there is no memory leak, you can create one)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325645734&siteId=291194637