记一次Android Studio Profiler使用,找到内存泄露原因

多的不说,直接上图:

bmp格式的图片不支持上传,差评,用手机拍下
1.先从reference 找到引用链

在这里插入图片描述

2.再从fields找到具体引用
在这里插入图片描述

UIThread 里的部分代码:

	@Override
    public void run() {
        try {
        	Looper.prepare();
    		if( runnable==null ){
    			iLog.w(TAG+" run() runnable is null");
    		}
    		
    		if( mHandler==null ){
    			iLog.w(TAG+" run() mHandler is null");
    		}
    		mHandler.obtainMessage(what_run_prepare).sendToTarget();
    		
        	runnable.run();//此处
        }
        catch(Exception e){
        	iLog.e(TAG+" run() "+e.getMessage());
        }
        finally{
	        mHandler.obtainMessage(what_run_completed).sendToTarget();
	        Looper.loop();
        }
	}


    Runnable runnable = () -> {
        try {
            doInBackground();
        } catch (Exception e) {
            iLog.w(TAG + " onStart UIThread exceptione=" + e.getMessage());
        }
    };

原因: Looper.loop();是死循环,相当于 UIThread里有个while(true) 一直在跑,所以会一直对activity有引用,activity不会被回收。

解决方法:那就是在activity 退出时 停止该线程 或 停止该线程中的looper 也相当于停止线程,不过不推荐直接用stop(),会报错,建议在内部声明个Looper mLooper 去获取 该线程的Looper , mLooper = Looper.myLooper(); 然后在destroy的时候 调用 mLooper.quit(); 就行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值