优化内存从以下几点搞起
- 内存泄漏 (未取消Handler发送的message、被静态引用)
- 大对象 (OOM)
3.频繁的波动,会引起频繁的GC有可能导致ANR
Memory Monitor:跟踪整个app的内存变化情况。
Heap Viewer:查看当前内存快照,便于对比分析哪些对象有可能发生了泄漏。
Allocation Tracker:追踪内存对象的来源。
http://hukai.me/android-performance-memory/
定位方式如下
12-08 08:47:24.064 7165-14687/com.sankuai.erp.cashier.debug D/dalvikvm: GC_FOR_ALLOC freed 3575K, 50% free 18579K/36504K, paused 31ms, total 31ms
12-08 08:47:24.396 7165-14688/com.sankuai.erp.cashier.debug D/dalvikvm: GC_FOR_ALLOC freed 5017K, 49% free 18831K/36504K, paused 44ms, total 44ms
12-08 08:47:25.112 7165-14687/com.sankuai.erp.cashier.debug D/dalvikvm: GC_FOR_ALLOC freed 5931K, 51% free 18225K/36504K, paused 13ms, total 13ms
12-08 08:47:26.192 7165-14688/com.sankuai.erp.cashier.debug D/dalvikvm: GC_FOR_ALLOC freed 4689K, 50% free 18519K/36504K, paused 24ms, total 25ms
12-08 08:47:28.840 7165-14689/com.sankuai.erp.cashier.debug D/dalvikvm: GC_FOR_ALLOC freed 5054K, 50% free 18514K/36504K, paused 12ms, total 12ms
12-08 08:47:29.808 7165-8839/com.sankuai.erp.cashier.debug V/PTR: 本地有打印机,但可用打印机列表为空,不进行轮询
12-08 08:47:31.396 7165-14689/com.sankuai.erp.cashier.debug D/dalvikvm: GC_FOR_ALLOC freed 5105K, 50% free 18523K/36504K, paused 10ms, total 10ms
12-08 08:47:35.948 7165-14684/com.sankuai.erp.cashier.debug D/uploadspeed: 10234.80432972523
12-08 08:47:35.980 7165-7165/com.sankuai.erp.cashier.debug W/EGL_genymotion: eglSurfaceAttrib not implemented
12-08 08:47:37.816 7165-8839/com.sankuai.erp.cashier.debug V/PTR: 本地有打印机,但可用打印机列表为空,不进行轮询
12-08 08:47:42.792 7165-8822/com.sankuai.erp.cashier.debug D/dalvikvm: GC_FOR_ALLOC freed 6278K, 53% free 17497K/36504K, paused 12ms, total 12ms
Dominator Tree: List the biggest objects and what they keep alive. 大Object列表,用于定位OOM。比如全家桶中Okhttp的 Cache。
内存泄漏
OOM
Class Name | Shallow Heap | Retained
Heap | Percentage
------------------------------------------------------------------------------------
okhttp3.internal.cache.DiskLruCache @ 0x12f50340| 88 | 56,704,448 | 81.34%
------------------------------------------------------------------------------------
内存耗用不一定存在内存泄漏,这是两个问题。