Leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
/**
* 获取LeakCanary用来监听内存泄漏的对象
*/
fun getRefWatcher() = mRefWatcher
/**
* LeakCanary
*/
private fun initLeakCanary() {
//LeakCanary.install()会返回一个预定义的 RefWatcher,同时也会启用一个ActivityRefWatcher,用于自动监控调用
//Activity.onDestroy()之后泄露的activity,如需在Fragment中监控,可在Fragment.onDestroy中使用refWatcher.watch()
mRefWatcher = LeakCanary.install(this)
}
baseactivity 和basefragment 的销毁中添加
HaoFaHuoApplication.getRefWatcher().watch(this)
集合里不用的item一定要删除,不然也会内存泄露
BroadcastReceiver和各种listener一定要在Activity里的onDestory里反注册
自定义的一些类需要context的时候不要直接用activity而使用activity.getApplicationContext()
各种adapter里要重用convertView,不然内存会被耗尽切无法回收
使用各种推送SDK用来更新UI的时候,在各个activity里注册的时候使用weakRef,防止泄露