Android的性能模式:救援技巧

来源:互联网 发布:淘宝详情页编辑 编辑:程序博客网 时间:2024/05/23 10:22

附上原文地址 Android Performance Patterns: Rescue tips


Apps nowadays are all about fancy animations, complex transitions and custom views, and the user experience must be intuitive and similar as possible in any device. These patterns gonna help you build an app that’s smooth, responsive, and uses as little battery as possible, it covers micro-optimizations that can improve overall app performance.


时下应用程序都需要花哨的动画,过渡的复杂和自定义视图,而且用户体验必须是直观,尽可能在任何设备都是相似的。这些模式会帮你建立一个应用程序,是平稳,反应灵敏,并在使用时尽可能的减少电池消耗,它涵盖了细节优化,可以提高整体应用性能。


Patterns to avoid bad performance

避免不良表现的模式

  • Avoid blocking main Thread
  • 避免阻塞主线程(避免ANR)
  • Avoid unnecessary invalidations that may trigger a cascade of invalidations
  • 避免无效的绑定无用的组件
  • Use RelativeLayouts in a high hierarchy level
  • 在多层级布局中尽可能的使用RelativeLayouts 
  • Avoid Nested weights in LinearLayouts ( Cz each child needs to be measured twice)
  • 避免使用LinearLayouts 的weight属性 (他的每个子视图在显示时都会测量两次)
  • Avoid Customs views not properly made
  • 避免使用不正确的自定义视图
  • Avoid Creating Unnecessary Objects
  • 避免创建无效的对象
  • Use Static Final For Constants (static 15%-20% faster)
  • 使用 Static Final (静态 恒定)修饰常量(能快15%-20% 
  • Primitives (Integer vs Float 2x slower)
  • 使用基本类型(Float 比Integer 慢两倍?注:感觉有歧义
  • Avoid Internal Getters/Setters (direct field access 3x faster)
  • 避免在内部类中使用Getters/Setters(字段访问速度会快3倍)
  • Use Enhanced For Loop Syntax
  • 使用增强的for循环语法
  • Consider Package Instead of Private Access with Private Inner Classes
  • 考虑使用Package去代替私有内部类
  • Use Native Methods Carefully
  • 使用Native时要小心(NDK、JNI的Native

Patterns for Custom views

自定义视图模式
  • Keep It as Simple as Possible (KISS)
  • 尽可能的保持简单(KISS原则)
  • Use merge tag as root in your layout (avoid extra ViewGroups on inflation)
  • 在根layout中使用merge(避免扩展性的ViewGroup)
  • include tag (easy code reuse of common layouts)
  • 使用include标签
  • Avoid unnecessary layouts
  • 避免无用的layout
  • Don’t make allocations or heavy operations in OnDraw
  • 不要在OnDraw中进行复杂操作及计算
  • Remove unnecessary calls to invalidate()
  • 移除不必要的invalidate()
  • Consider creating your own ViewGroup
  • 考虑创建属于你自己的ViewGroup
  • RecyclerView (replaces ListView and GridView)
  • RecyclerView (代替ListView和GridView)

Patterns to avoid memory churn

  • Don’t allocate a large amount of unnecessary objects:
  1. Immutable classes: String
  2. Autoboxing: Integer, Boolean..
  • Consider using Object Pools and caches to reduce churn
  • Be mindful of the overhead of enums (a single reference to an enum constant occupy 4 bytes)

Patterns to avoid memory leaks

避免内存泄漏的模式
  • Don’t leak contexts in inner classes
  • 不要泄漏内部类环境
  • Don’t leak views inside activity
  • 不要在Activity中泄漏View
  • Favor static inner classes over non static
  • 尽量使用静态内部类
  • Don’t use WeakHashmap as cache. Only the keys are WeakReference
  • 不要用WeakHashmap 作为缓存,这样只有Key是弱引用

Patterns for CPU

CPU模式
  • Do not nest multi-pass layouts
  • 不要窝多通道布局(注:不太明白)
  • Lazily compute complex data when needed
  • 当用的时候再去懒散的计算复杂数据(Lazily? 懒惰的意思?难道是随用随算的意思)
  • Cache heavy computational results for re-use
  • 重用缓存重计算结果
  • Consider RenderScript for performance
  • 考虑RenderScript性能
  • Keep work off of the main thread
  • 保证主线程是无工作的状态(注:确保主线程能随时工作)

Patterns to avoid Overdraw

避免重绘模式
  • Simplify your drawables
  • 简化你的绘制
  • Use nine patch with transparent parts
  • 用9-path绘制透明部分
  • Caution with setting alpha in your views
  • 谨慎的给View设置透明度
  • Remove unused background from your views
  • 移除无用的背景色

Patterns for Bitmaps

bitmap模式
  • Decode bitmaps to the desire size: BitmapFactory.Options(inSampleSize, inDensity, inTargetDensity)
  • 解码bitmap到希望的大小,BitmapFactory.Option
  • Load bitmaps in memory at the dimensions is going to be displayed
  • 内存中的bitmap是准备显示的(bitmap的缓存)
  • Don’t scale if you don’t need to (createScaledBitmap(btimap, int, int))
  • 如果你不需要bitmap就不要创建设定尺寸(用createScaledBitmap)
  • Use LRU cache
  • 用LRU缓存

Patterns for Services

Service模式
  • Do not keep running service unless it’s actively performing a job. Also be careful to stop service it when its work is done
  • 注意停止你的Service除非他还在工作。当他工作结束时也要注意去停止Service
  • System prefers to always keep the service process in running. Then the RAM used by the service can’t be used by anything else or paged out
  • 系统倾向于保持service的运行,当内存被service占用而不能被其他组件使用或换出时
  • The best way to limit the lifespan of your service is to use anIntentService, which finishes itself as soon as it’s done handling the intent that started it
  • 最好的方式是限制你的service的寿命(生命周期)而去使用IntentService,当他处理完启动Intent后会尽可能快的结束自己。
  • Leaving a service running when it’s not needed is one of the worst memory management mistakes an Android app can make
  • 保留一个不必要的service运行是一个糟糕的Android app内存管理失误

Patterns for Threads

线程模式
  • In a Thread run() method use Process.setThreadPriority() withTHREAD_PRIORITY_BACKGROUND. This approach reduces resource competition between the Runnable object’s thread and the UI thread.
  • 在一个线程的run()方法使用Process.setThreadPriority()withTHREAD_PRIORITY_BACKGROUND。这种方法减少了Runnable对象的线程和UI线程之间的资源竞争
  • If you don’t set the thread to a lower priority this way, then the thread could still slow down your app because it operates at the same priority as the UI thread by default.
  • 如果没有线程设置为较低的优先级这种方式,那么线程会拖慢你的app,因为默认普通线程的操作权限和UI线程是一样的
  • Stores the current Thread reference in your app, so that you want to interrupt the Thread later on. e.g: On network failure you can cancel that thread operation.
  • 存储在您的应用程序当前线程引用,以至于以后可以中断线程。例如:在网络出现故障,你可以取消线程操作。

Patterns to avoid ARNs

避免无响应模式
  • On UI thread do as little work as possible
  • 在UI线程尽可能少的执行任务
  • If your app is doing work in the background in response to user input, show that progress is being made (such as with a ProgressBar in your UI).
  • 如果你的app正在后台相应用户的输入,让用户看到通讯框(转圈圈)
  • Use performance tools such as Systrace and Traceview to determinebottlenecks in your app’s responsiveness.
  • 使用性能工具,如Systrace和Traceview以确定您的应用程序的响应能力的瓶颈。
  • If your application has a time-consuming initial setup phase, consider showing a launch screen or rendering the main view as quickly as possible, indicate that loading is in progress and fill the informationasynchronously.
  • 如果你的应用有一个耗时的初始设置阶段,考虑呈现出启动屏幕或尽可能快地渲染主视图,显示加载中并异步填充信息



1 0
原创粉丝点击