關(guān)于內(nèi)存泄漏,首先在工程里安裝相應(yīng)插件喜爷,在點(diǎn)到相應(yīng)頁面的時(shí)候則會(huì)有一個(gè)工具出現(xiàn)。
1. OrderServiceDialog?
Excluded by rule matching field android.os.Message#obj because Prior to ART , a thread waiting on a blocking queue will leak the last dequeued object as a stack local reference .
So when a HandlerThread becomes idle, it keeps a local reference to the last message it received. that message then gets recycled and can be used again. as long as all message are recycled after beingused, this won’t be a problem , because these references are cleared when beingrecycled. However, dialogs create when amessage needs to be sent.
These Message templates holds references to the dialoglisteners , which most likely leads to holding a reference onto the activity in someway.Dialogs never recycle their template Message, assuming these Message instances will get GCed when the dialog is GCed. the combination of the these two things creates a hingh potential for memory leaks as soon as you use dialogs. these memory leaks might be temporary, but some handler threads sleep for a long time.
To fix this, you could post empty messages to the idle handler threads from time to time. this won’t be easy because you cannot access all handler threads, but a librarythat is widely used should consider doing this for its own handler threads.
排除規(guī)則匹配字段android.os.Message#obj因?yàn)樵贏RT之前,等待阻塞隊(duì)列的線程將泄漏最后一個(gè)出隊(duì)對(duì)象作為堆棧本地引用。
所以當(dāng)HandlerThread變得空閑時(shí)崔挖,它會(huì)保留對(duì)它接收到的最后一個(gè)消息的本地引用贸街。該消息然后被再次使用庵寞,可以再次使用。只要所有消息在被使用后被回收薛匪,這不會(huì)是一個(gè)問題捐川,因?yàn)檫@些引用在被重新循環(huán)時(shí)被清除。但是逸尖,當(dāng)需要發(fā)送消息時(shí)會(huì)創(chuàng)建對(duì)話框古沥。
這些Message模板保存對(duì)dialoglisteners的引用瘸右,這最有可能導(dǎo)致在某個(gè)操作中保留對(duì)該活動(dòng)的引用.Dialogs從不回收其模板Message,假設(shè)當(dāng)對(duì)話框?yàn)镚Ced時(shí)岩齿,這些Message實(shí)例將被GCed太颤。一旦你使用對(duì)話框,這兩個(gè)東西的組合就會(huì)產(chǎn)生一個(gè)內(nèi)存泄漏的潛力盹沈。這些內(nèi)存泄漏可能是暫時(shí)的龄章,但是一些處理程序線程可以長(zhǎng)時(shí)間睡眠。
要解決這個(gè)問題乞封,您可以不時(shí)向空閑的處理程序線程發(fā)送空消息做裙。這不容易,因?yàn)槟鸁o法訪問所有處理程序線程肃晚,但是一個(gè)被廣泛使用的庫(kù)應(yīng)該考慮為自己的處理程序線程執(zhí)行此操作锚贱。
解決方法:首先添加弱引用,
privateDialogInterface.OnDismissListeneronDismissListener=newDialogInterface.OnDismissListener() {
@Override
public voidonDismiss(DialogInterface dialog) {
dialog.dismiss();
mHandler.sendEmptyMessage(0);
mHandler.removeCallbacksAndMessages(null);
}
};
private static classUpdateHandlerextendsHandler {
WeakReferenceweakReference;
UpdateHandler(OrderServiceDialog dialog) {
weakReference=newWeakReference<>(dialog);
}
@Override
public voidhandleMessage(Message msg) {
super.handleMessage(msg);
OrderServiceDialog dialog =weakReference.get();
if(dialog !=null) {
dialog.selectDateAdapter.cancleSelectUpdateAdapter();
}
}
}
發(fā)送一個(gè)空message給handler关串,然后把回調(diào)的message都移除掉拧廊。
2. 自定義的一個(gè)StickerSpan繼承了ImageSpan,將activity傳入晋修,導(dǎo)致了內(nèi)存泄漏卦绣。解決辦法:其實(shí)不需要activity,不再傳activity飞蚓。加入真的需要上下文滤港,則最好傳入getApplicationContext()。
3.
4. ?
5.
6.
7.
8.
9. ?
10.
11.
12.
13.
14.
15
16.