Stuck at “Dumping memory, app will freeze. Brrr.” message

亡梦爱人 提交于 2019-12-03 09:27:05

If you're on Android M you need to grant the "write external storage" permission or leak canary will hang for a long time with the brrr message. In your apps drawer, long click on the launcher for leak canary (like you were going to uninstall it) and drag up to "app info" and turn on the storage permission.

You should add the RefWatcher to your fragment as well just like what is described on the project page: https://github.com/square/leakcanary

LeakCanary.install() returns a pre configured RefWatcher. It also installs an ActivityRefWatcher that automatically detects if an activity is leaking after Activity.onDestroy() has been called.

public class ExampleApplication extends Application {

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

  private RefWatcher refWatcher;

  @Override public void onCreate() {
    super.onCreate();
    refWatcher = LeakCanary.install(this);
  }
}

You could use the RefWatcher to watch for fragment leaks:

public abstract class BaseFragment extends Fragment {

  @Override public void onDestroy() {
    super.onDestroy();
    RefWatcher refWatcher = ExampleApplication.getRefWatcher(getActivity());
    refWatcher.watch(this);
  }
}

Besides, if you want to get the heap dump when memory leak happened, just open the Android Device Monitor from Android Studio, and select the tab "File Explorer". In the directory /mnt/shell/emulated/0/Download/leakcanary/detected_leaks, you will find all the heap dump files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!