【问题标题】:LeakCanary doesn't catch leaks that I have deliberately added. What am I missing?LeakCanary 不会捕获我故意添加的泄漏。我错过了什么?
【发布时间】:2016-08-02 22:14:00
【问题描述】:

我们集成了 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 对象的引用仍然存在?

【问题讨论】:

    标签: android memory-leaks leakcanary


    【解决方案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'
    

    【讨论】:

    • 确实修复了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2016-01-01
    • 2015-11-08
    • 1970-01-01
    相关资源
    最近更新 更多