如何使用泄漏金丝雀 [英] How to use Leak Canary

查看:176
本文介绍了如何使用泄漏金丝雀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个愚蠢的问题,但我是开发android的新手,我目前在我的应用程序中遇到了OutOfMemoryError,我试图使用MAT调试,但是仍然很难找到泄漏在一些活动中,我找到了LeakCanary,这看起来更简单,更容易使用,但是我找不到任何有关使用Leak Canary的初学者一步一步指南,即使在Google上也是如此。我已经通过build.gradle中的依赖项安装了LeakCanary,这是我到目前为止所做的:

I know this is probably a dumb question, but I am pretty new at developing android, and I currently experiencing an OutOfMemoryError in my apps, which I have tried to debug using MAT, but it is still too hard to find the leak in a few activities, then I found LeakCanary, which seems simpler and easier to use, however I could not find any beginner step by step guide on using Leak Canary, even on Google. I have installed LeakCanary through the dependencies in my build.gradle, and this is what I got so far :

ExampleApplication.java

ExampleApplication.java

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

    final class KeyedWeakReference extends WeakReference<Object> {
        public final String key;
        public final String name;

        KeyedWeakReference(Object referent, String key, String name,
                       ReferenceQueue<Object> referenceQueue) {
            super(checkNotNull(referent, "referent"), checkNotNull(referenceQueue, "referenceQueue"));
            this.key = checkNotNull(key, "key");
            this.name = checkNotNull(name, "name");
        }
    }

    public void watch(Object watchedReference, String referenceName) {
        checkNotNull(watchedReference, "watchReference");
        checkNotNull(referenceName, "referenceName");
        if(debuggerControl.isDebuggerAttached()) {
            return;
        }
        final long watchStartNanoTime = System.nanoTime();
        String key = UUID.randomUUID().toString();
        retainedKeys.add(key);
        final KeyedWeakReference reference =
            new KeyedWeakReference(watchedReference, key, referenceName, queue);
        watchExecutor.execute()

    }
}

假设我有一个活动,我希望LeakCanary看一个对象

Let's say I have an Activity where I want LeakCanary to watch an object

SampleActivity.java

SampleActivity.java

public class SampleActivity extends Activity implements View.OnClickListener {
    ImageView level001, level002;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_level);

        level001 = (ImageView) findViewById(R.id.level001);
        level002 = (ImageView) findViewById(R.id.level002);

        // Do all kinds of function
        // How do I use LeakCanary to watch these objects ?

    }
}

现在我如何使用LeakCanary来看哪个对象导致内存泄漏。感谢您的帮助和帮助。

Now how do I use LeakCanary to see which object is causing the memory leak. Thank you for your help and assistance.

推荐答案

关于泄漏金丝雀的好处是它的自动化程度。
默认情况下,它已经监视未正确GCed的活动。因此,开箱即用,如果有任何活动泄漏,您应该收到通知。

The nice thing about leak canary is how automated it works. By default, it already "watches" for activities that are not being properly GCed. So out of the box, if any activity is leaking you should receive the notification.

在我的项目中,我在上添加了额外的方法应用程序,如下所示:

On my project I've added an extra method on the Application like this:

public class ExampleApplication extends Application {
    public static ExampleApplication instance;
    private RefWatcher refWatcher;

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

    public void mustDie(Object object) {
        if (refWatcher != null) {
            refWatcher.watch(object);
        }
    }
}

所以垃圾的重要内容收集和内存泄漏和金丝雀是知道应该收集什么东西并要求观看该项目。

so the important stuff with garbage collection and memory leak and canary is to know when stuff should be collected and ask that item to be watched.

例如我们使用的是基本片段以下代码:

For for example we're using a "base fragment" with the following code:

@Override
public void onDestroy() {
    super.onDestroy();
    ExampleApplication.instance.mustDie(this);
}

这样 LeakCanary 正在尝试检查是否有任何片段泄漏内存。

this way LeakCanary is trying to check if any fragment is leaking memory.

因此,为了在您的应用上进一步实施,您可以/应该在您知道它应该是的任务或实例上垃圾收集但你认为它可能不是,你不确定在哪里,你也可以调用它: ExampleApplication.instance.mustDie(object);

So for you to further implement on your app, you could/should on tasks or instances that you know it should be garbage collected but you think it might not be, and you're not sure where, you can call that too: ExampleApplication.instance.mustDie(object);

然后你必须运行应用程序并旋转设备并强制泄漏发生,因此泄漏金丝雀可以抓取/分析堆栈跟踪并为您提供有关如何修复它的宝贵信息。

and then you MUST run the application and rotate the device and force the leak to happen, so leak canary can grab/analyse the stack trace and give you valuable information on how to fix it.

我希望它有所帮助。

这篇关于如何使用泄漏金丝雀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆