leakcanary 内存泄漏使用并注意

    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,防止泄露
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值