操作,打开activity,然后后退键退出,10几秒后就收到内存泄露的提示
日志:
D/LeakCanary: In com.example.shen.testleak:1.0:1.
* com.example.shen.testleak.TestLeakActivity has leaked:
* GC ROOT static android.media.AudioManager.mContext_static
* references android.app.ContextImpl.mOuterContext
* leaks com.example.shen.testleak.TestLeakActivity instance
W/ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1468 com.android.server.StatusBarManagerService.sendNotification:968 com.android.server.StatusBarManagerService.addNotification:657 com.android.server.NotificationManagerService$6.run:2115 android.os.Handler.handleCallback:733
我的配置过程
1.在
build.gradle
中加入引用
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
}
2.在 Application 内配置,安装
public class MyApplication extends Application {
private RefWatcher refWatcher;
public static RefWatcher getRefWatcher(Context context) {
MyApplication application = (MyApplication) context.getApplicationContext();
return application.refWatcher;
}
@Override
public void onCreate() {
super.onCreate();
refWatcher = LeakCanary.install(this);
}
}
3.TestLeakActivity 的 onDestroy() 内添加watch
public class TestLeakActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = MyApplication.getRefWatcher(this);
refWatcher.watch(this);//根据,回答者提醒把这段话放onDestroy后面,重新编译了一下,可惜错误依旧
}
}
又等了一会,提示DisplayLeakActivity内存泄漏
D/LeakCanary: In com.example.shen.testleak:1.0:1.
* com.squareup.leakcanary.internal.DisplayLeakActivity has leaked:
* GC ROOT static android.media.AudioManager.mContext_static
* references android.app.ContextImpl.mOuterContext
* leaks com.squareup.leakcanary.internal.DisplayLeakActivity instance
请问是否是我的配置有问题?
但是看日志反馈的是泄漏了啊,说不定android源码里面有泄漏呢。配置很简单,配置很难出问题。