31

I'm trying to use LeakCanary to detect memory leaks in my app, but it does not go further than the message "Dumping memory, app will freeze. Brrr." I've been waiting for about 20 minutes or so, but no changes. Same behaviour on these devices: 1. Asus fonepad 8 (Android 5.0 stock) 2. Sony Xperia SP (Android 5.1.1 CM 12.1 custom) 3. HTC Desire C (Android 4.4 CM 11 custom)

I did everything as its advised in instruction:

public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    LeakCanary.install(this);
  }
}
Sergey Maslov
  • 758
  • 1
  • 8
  • 22

2 Answers2

42

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.

bsautner
  • 4,479
  • 1
  • 36
  • 50
  • 3
    Android M is slowly becoming the bane of my existence. I didn't even suspect that this would be the issue, but it was. There is an open issue for this on GitHub so hopefully it gets fixed in a future release. – CodyEngel Nov 29 '15 at 00:22
  • 4
    @bsautner This is fixed in `1.4-beta1` version of Leakcanary. [Handle storage permission on M](https://github.com/square/leakcanary/commit/ea0af1d6be08ba4ac7eda913219456286ab0c4f8) : `When a potential leak is detected, if the storage permission is missing, we drop the leak and we show a notification. That notification will then show the permission dialog.` – blizzard Jan 19 '16 at 11:05
  • What if I'm not on M and still get this error? I have an app running on L with both read and write external storage permissions and still see this annoying dialog. Any suggestions? – Joao Sousa Feb 16 '17 at 09:29
  • Getting this error on N with no way to enable the permission though it exists in the manifest. – tricknology May 04 '17 at 20:36
  • This is a pretty old problem. Maybe update your version of leak Canary? I think they fixed this. – bsautner May 04 '17 at 21:01
  • Got the same in Android4.2.2. – Lazy Ninja Jun 23 '17 at 01:57
  • Nope. Nothing fixed. Gave both read and write permissions. Flipped the permission on in Settings. Tried requesting permission. Tried everything. Still get the error: "Please give external storage permission..." on my phone and can't get memory leaks. Android is a nightmare. – datWooWoo Apr 24 '18 at 06:27
-2

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.

enter image description here

motou
  • 726
  • 4
  • 12
  • This doesn't answer the OP's question. – Alex Lockwood Aug 02 '15 at 05:19
  • Please check if the refWatcher is used correctly in Fragments. If not, one cannot tell if a memory leak is really happened, because the message "Dumping memory, app will freeze. Brrr." means only a "suspected" memory leak with a heap dump. – motou Aug 03 '15 at 09:54
  • 3
    Sorry if I am misunderstanding... the OP didn't mention anything specifically about Fragments. In their case (and mine too), all we are doing is putting the `LeakCanary.install(this)` line in the `Application#onCreate()` method. – Alex Lockwood Aug 03 '15 at 12:47