2016-08-02 55 views
3

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

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对象的引用会一直存在?

回答

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' 
+1

的确是固定的。 –