最近开发app做到相册模块,发现反复进入相册的Fragment后,内存不断升高,最后OOM
通过LeakCanary工具定位问题,发现内存泄漏发生在Thread中
logcat如下:
06-01 21:41:24.855 23242-26049/mzh.plantcamera D/LeakCanary: In mzh.plantcamera:1.0:1.
* mzh.plantcamera.ui.activity.PlantAlbumActivity has leaked:
* GC ROOT thread mzh.plantcamera.model.impl.PlantAlbumModelImpl$ScanDBThread.this$0 (named 'Thread-6767')
* references mzh.plantcamera.model.impl.PlantAlbumModelImpl.onScanListener
* references mzh.plantcamera.Presenter.impl.PlantAlbumPresenterImpl$1.this$0 (anonymous class implements mzh.plantcamera.model.PlantAlbumModel$OnScanListener)
* references mzh.plantcamera.Presenter.impl.PlantAlbumPresenterImpl.plantAlbumView
* leaks mzh.plantcamera.ui.activity.PlantAlbumActivity instance
最后查明原因是在Thread中调用Looper.prepare()为线程开启自己的消息循环,在Looper.loop();之后没有在适当的地方进行quit,导致线程一直没有销毁
从而导致Thread外部类的引用,而外部类的引用最终持有了Activity的引用,导致Activity在退出后没有被销毁,导致内存泄漏
网上查明资料后得知
Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用Handler.getLooper().quit()后,loop才会终止。
loop()函数的源码中也能看出需要quit,提示如下:
Run the message queue in this thread. Be sure to call quit() to end the loop
不得不说LeakCanary是神器啊~