Android内存优化的5R法则 胡凯 Android
hukai.me 腾讯
内存管理基础 – 共享内存 K Linear I Zygote Alloc V L Alloc A Space Space new ArrayList D Space Non Main Large T Zygote Image R Moving Alloc Obj A Space Space Space Space Space
内存管理基础 – 内存回收 Not enough room GCGC
内存管理基础 - 内存回收
内存管理基础 - 内存回收
内存管理基础 - 内存泄漏 Memory Leaks Happen Here Unreferenced Referenced Objects Objects Unused Objects
内存管理基础 - 内存泄漏
内存管理基础 - 内存泄漏 Leaked Allocated Needed Memory Memory Memory Leaked Allocated Needed Memory Memory Memory
内存管理基础 - 内存分析
内存管理基础 - 内存分析
内存管理基础 - 内存分析
内存管理基础 - 内存分析
内存管理基础 - 内存分析
内存管理基础 - 内存分析 More Free Space
Less Garbage Collection 1 Reduce 2 Reuse 3 Recycle 4 Refactor 5 Revalue 1
Reduce
1 )差异化对待- 缓存容量 Nexus 4 Nexus 5 Nexus 6
1 )差异化对待- 图片资源 xhdpi xxhdpi xxxhdpi
2 )资源按需加载
3 )减小Bitmap对象的内存占用
3 )减小Bitmap对象的内存占用- inSampleSize
3 )减小Bitmap对象的内存占用 – decode format ARGB_8888 : 32 Bits Per Pixel RBG_565 : 16 B ARGB_4444 : 16 B ALPHA_8 : 8 B 2
Reuse
1 )Pools
1 )Pools
2 )Bitmap对象的复用 - inBitmap
2 )Bitmap对象的复用 - inBitmap
3 )注意在ListView/GridView中ConvertView的复用 Item 0 Item 0 Item 1 Item 1 Item 1 Item 2 Item 2 Item 2 Item 3 Item 3 Item 3 Item 4 Item 4 Item 4 Item 5 Item 0 getView Item 5 Recycler
4 )避免频繁的创建与回收对象 16ms 16ms 16ms onDraw onDraw onDraw Memory Memory Memory Heap Heap Heap
4 )避免频繁的创建与回收对象 3
Recycle
1 )Activity泄漏 - 内部类
1 )Activity泄漏 - 容器对象泄漏
1 )Activity泄漏 - Static, Singleton Life of your app process Life of static variable Life of singleton instance Life of some containers Life of Activity A Life of Activity B Dinosaur Life of Activity C
2 )谨慎选择合适的Context Content Broadcast Application Activity Service Provider Receiver Show a Dialog NO YES NO NO NO Start an Activity NO1 YES NO1 NO1 NO1 Layout Inflation NO2 YES NO2 NO2 NO2 Start a Service YES YES YES YES YES Bind to a Service YES YES YES YES NO Send a Broadcast YES YES YES YES YES Register YES YES YES YES NO3 BroadcastReceiver Load Resource YES YES YES YES YES Values
3 )注意有生命周期对象的注销 register unregister create destr