Reputation: 557
I want to add leak canary to a test project. I created a project and did the steps from this video: https://www.youtube.com/watch?v=2VKBjlHtKMY
When I'm trying wrongWay()
, the message "Dumping Memory app will freeze" is appearing in the emulator Nexus_5X_API_23. When I'm trying rightWay()
, the same message is appearing too. There is no message "Dumping Memory app will freeze" in video when author using rightWay()
.
I cant understand why?
Where can I find a good tutorial about Leak Canary or other Leak Memory library.
So build.gradle is:
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();
}
}
Upvotes: 3
Views: 5379
Reputation: 756
I think you're doing the rightWay() wrong.
MyThread should be static, otherwise it's a non-static inner class, and implicitly references the outer class Activity. The thread runs forever, so the activity won't be GCed. This is a memory leak.
Upvotes: 0
Reputation: 91
I think you should add the following to your Activity onDestroy method
RefWatcher refWatcher = MyApplication.getRefWatcher(this);
refWatcher.watch(this);
You can refer to following URL https://github.com/square/leakcanary/wiki/FAQ
Upvotes: 1