泄漏金丝雀不起作用

时间:2016-09-09 09:05:36

标签: java android memory-leaks leakcanary

我想在测试项目中添加泄漏金丝雀。我创建了一个项目并完成了此视频中的步骤:https://www.youtube.com/watch?v=2VKBjlHtKMY

当我尝试wrongWay()时,消息"转储内存应用将冻结" 出现在模拟器Nexus_5X_API_23中。当我尝试rightWay()时,同样的消息也出现了。没有消息"当作者使用rightWay()时,倾销记忆应用会在视频中冻结"

我不明白为什么?

我在哪里可以找到关于Leak Canary或其他Leak Memory库的好教程。

所以build.gradle是:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
        applicationId "com.example.vopolski.myleakcanary"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}

的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vopolski.myleakcanary">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".LeakCanaryApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

LeakCanaryApplication.java

package com.example.vopolski.myleakcanary;

import android.app.Application;
import android.content.Context;
import android.os.SystemClock;

import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

public class LeakCanaryApplication extends Application {

    private RefWatcher refWatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        refWatcher = LeakCanary.install(this);
    }

    public static RefWatcher getRefWatcher(Context context) {
        LeakCanaryApplication application =
                (LeakCanaryApplication) context.getApplicationContext();
        return application.refWatcher;
    }
}

MainActivity.java

package com.example.vopolski.myleakcanary;

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import android.app.Application;

public class MainActivity extends AppCompatActivity {

    private String msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rightWay();


    }

    private void rightWay() {
        new MyThread().start();
    }

    private class MyThread extends Thread {
        @Override
        public void run() {
            while (true) {
                SystemClock.sleep(1000);
            }
        }
    }

    private void wrongWay() {
        new Thread() {
            @Override
            public void run() {
                while (true){
                    SystemClock.sleep(1000);
                }
            }
        }.start();
    }
}

2 个答案:

答案 0 :(得分:1)

我认为你应该将以下内容添加到Activity onDestroy方法

RefWatcher refWatcher = MyApplication.getRefWatcher(this);
refWatcher.watch(this);

您可以参考以下网址https://github.com/square/leakcanary/wiki/FAQ

答案 1 :(得分:0)

我认为你做错了对错(

MyThread应该是静态的,否则它是一个非静态内部类,并隐式引用外部类Activity。线程永远运行,因此活动不会被GCed。这是内存泄漏。