Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is my leakcanary working? How to know?

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();
}

}

like image 642
seekingStillness Avatar asked Dec 31 '16 18:12

seekingStillness


People also ask

How do I stop LeakCanary?

Disable the leakcanary launcher activity by setting the leak_canary_add_launcher_icon resource boolean to false.

How does leak Canary work?

LeakCanary hooks into the Android lifecycle to automatically detect when activities and fragments are destroyed and should be garbage collected. These destroyed objects are passed to an ObjectWatcher , which holds weak references to them.

What is leak Canary Android?

LeakCanary is a memory leak detection library for Android. LeakCanary's knowledge of the internals of the Android Framework gives it a unique ability to narrow down the cause of each leak, helping developers dramatically reduce Application Not Responding freezes and OutOfMemoryError crashes. Get started!

How do you identify memory leakage in an Android application?

The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.


2 Answers

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.

like image 131
CommonsWare Avatar answered Sep 18 '22 01:09

CommonsWare


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...

like image 25
Paul Spiesberger Avatar answered Sep 19 '22 01:09

Paul Spiesberger