Android内存泄漏检测-LeakCanary

square/leakcanary
udacity/Sunshine-Version-2

添加LeakCanary依赖包

在主模块app下的build.gradle下添加如下依赖:

debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'

这里写图片描述

开启LeakCanary

添加Application子类

首先创建一个ExampleApplication,该类继承于Application,在该类的onCreate方法中添加如下代码开启LeakCanary监控:

LeakCanary.install(this);

这里写图片描述

在配置文件中注册ExampleApplication

AndroidManifest.xml中的application标签中添加如下信息:

android:name=".ExampleApplication"

这里写图片描述

这个时候安装应用到手机,会自动安装一个Leaks应用,如下图:
这里写图片描述

制造一个内存泄漏的点

建立一个ActivityManager类,单例模式,里面有一个数组用来保存Activity

package com.example.android.sunshine.app;

import android.app.Activity;
import android.util.SparseArray;
import android.view.animation.AccelerateInterpolator;

import java.util.List;

/**
 * Created by wuxian on 15/10/23.
 */
public class ActivityManager {
    private SparseArray<Activity> container = new SparseArray<Activity>();
    private int key = 0;
    private static ActivityManager mInstance;

    private ActivityManager(){}

    public static ActivityManager getInstance(){
        if(mInstance == null){
            mInstance = new ActivityManager();
        }

        return mInstance;
    }

    public void addActivity(Activity activity){
        container.put(key++,activity);
    }


}

然后在DetailActivity中的onCreate方法中将当前activity添加到ActivityManager的数组中:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ActivityManager.getInstance().addActivity(this);
        if (savedInstanceState == null) {
            // Create the detail fragment and add it to the activity
            // using a fragment transaction.

            Bundle arguments = new Bundle();
            arguments.putParcelable(DetailFragment.DETAIL_URI, getIntent().getData());

            DetailFragment fragment = new DetailFragment();
            fragment.setArguments(arguments);

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.weather_detail_container, fragment)
                    .commit();
        }
    }

我们从首页跳转到详情页的时候会进入DetailActivityonCreate的方法,然后就将当前activity添加到了数组中,当返回时,我们没把他从数组中删除。再次进入的时候,会创建新的activity 并添加到数组中,但是之前的activity仍然被引用,无法释放,但是这个activity不会再被使用,这个时候就造成了内存泄漏。我们来看看LeakCanary是如何报出这个问题的。

演示

这里写图片描述

解析的过程有点耗时,所以需要等待一会才会在Leaks应用中,当我们点开某一个信息时,会看到详细的泄漏信息:
这里写图片描述

Program type already present问题应该怎么解决

06-27
根据网上的搜索信息改成了27.1.1,但问题还没有解决,请问该怎么做? ``` apply plugin: 'com.android.application' buildscript { repositories { mavenCentral() jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' } allprojects { repositories { jcenter() } } } dependencies { implementation fileTree(include: '*.jar', dir: 'libs') // implementation project(':appcompat_v7') // implementation project(':recyclerview') implementation 'com.android.support:appcompat-v7:27.1.1' implementation project(':recyclerview') debugCompile 'me.ele:uetool:1.0.15' releaseCompile 'me.ele:uetool-no-op:1.0.15' // if you want to show more attrs about Fresco's DraweeView debugCompile 'me.ele:uetool-fresco:1.0.15' // // configurations { // all*.exclude group: 'com.android.support', module: 'support-v13' // } } android { compileSdkVersion 26 buildToolsVersion '26.0.2 ' useLibrary 'org.apache.http.legacy' compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false lintOptions { abortOnError false } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['jniLibs']; } androidTest .setRoot('tests') // Move the build types to build-types/<type> // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... // This moves them out of them default location under src/<type>/... which would // conflict with src/ being used by the main source set. // Adding new build types or product flavors should be accompanied // by a similar customization. debug.setRoot('build-types/debug') release.setRoot('build-types/release') } } repositories { google() } ```
©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值