1

I think code for FusedLocationProvider api leaks as i asked in this question. I tried using to detect memory leak with LeakCanary but i do not get any warnings for memory leak after i rotate my phone, also tried with a tablet. Code for setting LeakCanary:

public class MyApplication extends Application {
    private RefWatcher refWatcher;
    @Override
    public void onCreate() {
        super.onCreate();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        refWatcher = LeakCanary.install(this);
        // Normal app init code...
        Log.d("TAG", "MyApplication onCreate()");
    }
}

I tried over 20 times but 2 times i got notification from LeakCanary without even rotating the phone but memory leak that should occur after device is rotated.

I also have another question why new instances of MainActivity are created even if no memory leak occurs and no references to these MainActivities exist.

Device is rotated 2 times without starting location updates:

Image No Memory Leak

Device is rotated 2 times after starting location updates: Image with Memory Leak

Why does memory leaks occur if location updates are stopped onPause() with stopLocationUpdates() method?

Why does LeakCanary not detect these leaks?

Thracian
  • 43,021
  • 16
  • 133
  • 222

2 Answers2

0

You don't need to create a variable for the RefWatcher.

You can simply try this:

 @Override
public void onCreate() {
    super.onCreate();
    if (LeakCanary.isInAnalyzerProcess(this)) {
        // This process is dedicated to LeakCanary for heap analysis.
        // You should not init your app in this process.
        return;
    }
    LeakCanary.install(this);
    // Normal app init code...
}

Also make sure you have these inside of your gradle.

  debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3'
  releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
 //Optional, if you use support library fragments:
 debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3'
 testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
0

you should run your app again. because at first time that we launch debug version, canaryleak dosent recognize leakage. but close the app and run again it will recognize leakage(but be sure to give needed permission to canaryleak when its popup appear). goodluck

Mosayeb Masoumi
  • 481
  • 4
  • 10