java - 泄漏金丝雀不起作用

标签 java android memory-leaks leakcanary

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

当我尝试 wrongWay() 时,消息 “Dumping Memory app will freeze” 出现在模拟器 Nexus_5X_API_23 中。当我尝试 rightWay() 时,同样的消息也出现了。当作者使用 rightWay() 时,视频中没有消息“Dumping Memory app will freeze”

我不明白为什么?

在哪里可以找到有关 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;
    }
}

主 Activity .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();
    }
}

最佳答案

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

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

可以引用以下网址https://github.com/square/leakcanary/wiki/FAQ

关于java - 泄漏金丝雀不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39407810/

相关文章:

Java 字符串换行

java - iOS CoreImage Android 中的即时拍照效果,如何制作?

android - 如何从 ListView 中获取数据

C++ 内存泄漏问题

java - 打开用 Java 创建的 Excel 电子表格时出现输入/输出错误

java - 如果我的父类抛出异常,我的子类必须捕获它吗?

android - 如何在 Android 6 中添加多个权限并在模拟器中进行测试?

java - 为什么在 java 中 android 第二次调用 web 服务时会创建新的 session

c# - 托管代码中是否可能发生内存泄漏? (特别是 C# 3.0)

node.js - 在 Node.js 中使用流时内存泄漏?