目錄
一、內(nèi)存泄露檢測(cè)之LeakCanary
二、App卡頓檢測(cè)之BlockCanary
三、圖片加載庫(kù)之Glide
一、內(nèi)存泄露檢測(cè)之LeakCanary
github地址:https://github.com/square/leakcanary/
學(xué)習(xí)地址:https://square.github.io/leakcanary/changelog/#version-22-2020-02-05
使用和原理分析:
https://zhuanlan.zhihu.com/p/360944586
https://www.jb51.net/article/208385.htm#_lab2_2_2
關(guān)注點(diǎn):
1.LeakCananry自動(dòng)檢測(cè)步驟:
- 檢測(cè)可能泄漏的對(duì)象;
- 堆快照,生成hprof文件;
- 分析hprof文件;
- 對(duì)泄漏進(jìn)行分類。
2.不用主動(dòng)初始化,因?yàn)樽?cè)了contentProvider 在onCreate()時(shí)做了自動(dòng)初始化的邏輯。
3.主動(dòng)監(jiān)測(cè)的實(shí)現(xiàn)
- Activity的生命周期監(jiān)聽:注冊(cè) Application.ActivityLifecycleCallbacks ;
- Fragment的生命周期期監(jiān)聽:同樣注冊(cè)FragmentManager.FragmentLifecycleCallbacks
- 反射獲取 WindowManagerGlobal 的mViews屬性
用到了這個(gè)庫(kù) https://github.com/square/curtains
4.使用弱引用 來監(jiān)聽需要監(jiān)聽的對(duì)象,調(diào)動(dòng) Runtime.getRuntime.gc() 和 System.runFinalization() 來觸發(fā)垃圾回收。然后再循環(huán)判斷保存在隊(duì)列中弱引用是否為空。為空,則從map中移除掉。最后根據(jù)map中剩下的元素?cái)?shù)量來決定是否生成hprof文件,調(diào)用Debug.dumpHprofData()方法可以生成該文件。最后就是對(duì)hprof文件的。
System.runFinalization(); //強(qiáng)制調(diào)用已經(jīng)失去引用的對(duì)象的finalize方法
5.有興趣的話,可以自行了解hprof文件的分析處理過程。
二、App卡頓檢測(cè)之BlockCanary
github地址:https://github.com/markzhai/AndroidPerformanceMonitor
學(xué)習(xí)地址:http://blog.zhaiyifan.cn/2016/01/16/BlockCanaryTransparentPerformanceMonitor/
使用和原理分析:
http://blog.zhaiyifan.cn/2016/01/16/BlockCanaryTransparentPerformanceMonitor/
http://www.lxweimin.com/p/d172aafc3437
關(guān)注點(diǎn):
1.Looper 的loop()方法內(nèi)在循環(huán)從MessageQueue中獲取的消息來處理的前后預(yù)留了接口,如下圖所示,只要通過Looper.getMainLooper().setMessageLogging()來設(shè)置自定義的Printer,即可收到回調(diào),用戶就可以在回調(diào)中做一些cpu 、耗時(shí)等信息的收集。
public static void loop() {
...
for (;;) {
...
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
...
}
}
三、圖片加載庫(kù)之Glide
github地址:https://github.com/bumptech/glide
學(xué)習(xí)地址 :https://muyangmin.github.io/glide-docs-cn/
與其他圖片加載庫(kù)的對(duì)比:
http://www.lxweimin.com/p/97994c9693f9
https://blog.csdn.net/github_33304260/article/details/70213300
Glide源碼分析:
https://cloud.tencent.com/developer/article/1485133