泄漏金丝雀不会捕获我故意添加的泄漏。我错过了什么?



我们集成了LeakCanary,以帮助我们解决我们认为是内存泄漏的问题。为了确保我们把每件事都设置好,我创建了一个不会被删除的静态引用,并告诉leakCanary注意它,

public class Cat {
 }
 public class Box {
        public Cat hiddenCat;
 }
public class Docker {
     public static Box container;
}

MainActivity

    Box box;
    Cat schrod;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    box = new Box();
    schrod = new Cat();
    box.hiddenCat = schrod;
    Docker.container = box;
    Application.getRefWatcher().watch(schrod);

    System.gc();
}

Application类中

    private static RefWatcher sRefWatcher;
    @Override
    public void onCreate() {
        sRefWatcher = LeakCanary.install(this);  
    }       
    public static RefWatcher getRefWatcher() {
        return sRefWatcher;
    }

并且在build.gradle文件中:

 dependencies {
        debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
        releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
 }

我错过了什么,它从来没有告诉我对schrod物体的引用一直存在?

您可能需要将build.gradle依赖项更新到最新版本(可在本页的Getting Started指南中找到:https://github.com/square/leakcanary)恰好是:

debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'

相关内容

最新更新