11

I believe to have successfully installed LeakCanary.

I added the debug, release, and test dependencies to the build.gradle file.

I added the necessary files to my Application Class. Imported as necessary. Confirmed the application class is properly added to manifest. Does my application class need to be explicitly called?

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"

I run my app on the emulator and don't see anything different. I monitor the Android Monitor and don't see any difference. How do I know if it's all working? I've shared my Application class.

import android.app.Application;
import android.content.res.Configuration;
import com.squareup.leakcanary.LeakCanary;

public class MyApplication extends Application {

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

    if (LeakCanary.isInAnalyzerProcess(this)) {
        return;
    }
    LeakCanary.install(this);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
public void onLowMemory() {
    super.onLowMemory();
}

}

seekingStillness
  • 4,833
  • 5
  • 38
  • 68

2 Answers2

10

Does my application class need to be explicitly called?

No.

How do I know if it's all working?

Leak something intentionally. For example, assign your launcher activity instance to a static field.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I tried, but maybe I did it wrong. I googled common memory leaks and tried those two. No change. I must have done something wrong. What should I expect to see from leak canary if it's working correctly? – seekingStillness Dec 31 '16 at 20:34
  • @seeking_stillness: "I googled common memory leaks and tried those two" -- LeakCanary does not detect arbitrary memory leaks out of the box. The only thing it will detect automatically is a leaked activity. This is why I suggested that you leak an activity by assigning one to a `static` field. "What should I expect to see from leak canary if it's working correctly?" -- after an activity is destroyed (e.g., you press BACK), after a few seconds, if LeakCanary believes that the activity you destroyed was leaked, you will see a custom `Toast` appear, indicating that LeakCanary is dumping the heap. – CommonsWare Dec 31 '16 at 20:38
  • 1
    @seeking_stillness: Then, a minute or so later, if there was a leak, you should see a `Notification` in the status bar. – CommonsWare Dec 31 '16 at 20:38
  • Notification in status bar <---- Yes I see it now! – seekingStillness Dec 31 '16 at 20:57
3

First, check if you are attached to a debugger? LeakCanary ignores leak detection when debugging to avoid false positives.

Second, Add the LeakCanary via Gradle and then do the following:

class App : Application() {

    companion object {
        @JvmStatic
        fun getRefWatcher(context: Context): RefWatcher {
            val applicationContext = context.applicationContext as App
            return applicationContext.refWatcher
        }
    }

    private lateinit var refWatcher: RefWatcher

    override fun 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)
    }
}

In your Fragment (best you use a BaseFragment)

override fun onDestroy() {

   if (BuildConfig.DEBUG) {
      activity?.let {
         App.getRefWatcher(it).watch(this)
      }
      super.onDestroy()
}

Then in one of your Sub-Activities with a Fragment do:

class MemLeakFragment : BaseFragment() {

    companion object {
        @JvmStatic
        lateinit var memleak: Activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activity?.let {
            memleak = it
        }
    }
}

Open the Activity with the memleak Fragment and close it with a back press. Wait a bit and LeakCanary will report the memory leak, this can take a while...

Paul Spiesberger
  • 5,630
  • 1
  • 43
  • 53