3

我们集成了 LeakCanary 来帮助我们解决我们认为的内存泄漏问题。为了确保我们正确设置了所有内容,我创建了一个不会被删除的静态引用,并告诉泄漏Canary 观看它,

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 对象的引用仍然存在?

4

1 回答 1

1

您可能需要将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'
于 2016-08-02T23:26:03.800 回答