android - LeakCanary 不会捕获我故意添加的泄漏。我错过了什么?

标签 android memory-leaks leakcanary

我们集成了 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();
}

应用类中

    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'

关于android - LeakCanary 不会捕获我故意添加的泄漏。我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731322/

相关文章:

android - 拆分源代码 : ListView for phones/GridView for tablet

android - 如何以编程方式控制 ActionBar 拆分?

android - 如何通过 AVD 模拟特定的 Android 设备?

c++ - 使用/不使用 Visual Leak Detector 在 C++ 中检测内存泄漏

node.js - pm2 显示 1.2GB 而 heapdump 显示 80MB

android - 如何忽略 LeakCanary 中的某些类?

android - TabLayout 的标签不显示

android - 泄漏金丝雀检测 MediaBrowserServiceCompat 示例应用程序中的内存泄漏

java - 如何使用泄漏金丝雀

Python 内存占用与堆大小