多的不说,直接上图:

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

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

2.再从fields找到具体引用

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

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(); 就行。