Home > Java > javaTutorial > body text

Suggestions for improving android performance

伊谢尔伦
Release: 2016-12-03 09:07:41
Original
1098 people have browsed it

Everyone knows that the success of an app is closely related to the performance experience of the app. But how to make your app have the ultimate performance experience? In this talk from DroidCon NYC 2015, Boris Farber brings his experience with Android APIs and how to avoid some common pitfalls. Learn how to shorten startup time, optimize sliding effects, and create a smoother user experience.

Introduction

Hello everyone, I am Boris, now an employee of Google, currently focusing on apps that require high performance. This sharing is the best practice I have accumulated over time from my mistakes and when consulting with partners. If you have a small app, reading this will help you in the growth stage of your app.

I often see apps that take a long time to start, slide unsmoothly, or even become unresponsive. We usually spend a lot of time improving these problems, after all, we all want our apps to be successful.

Activity leak

The first problem we need to fix is ​​Activity leak. Let’s first take a look at how the memory leak occurs. Activity leaks are usually a type of memory leak. Why does it leak? If you hold a reference to an unused Activity, you actually hold the layout of the Activity, which naturally includes all Views. The trickiest thing is holding static references. Don't forget, Activity and Fragment have their own life cycle. Once we hold a static reference, the Activity and Fragment will not be cleaned up by the garbage collector. This is why static references are dangerous.

m_staticActivity = staticFragment.getActivity()
Copy after login

I’ve seen code like this too many times.

In addition, leaking Listener is also a common thing. For example, I have the following code. LeakActivity inherits from Activity. We have a singleton: NastyManager. When we bind Activity as a Listener and NastyManager through addListener(this), bad things happen.

public class LeakActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); NastyManager.getInstance().addListener(this);  
  }  
}
Copy after login

To fix this bug, it is actually quite simple. When your Activity is destroyed, just unbind it from NastyManager.

@Override public void onDestroy() { super.onDestroy();  
  NastyManager.getInstance().removeListener(this);  
}
Copy after login

Compared with the above solutions, we naturally have a better one. For example, do we really need to use singletons? Usually, it's not needed. But sometimes it may be really necessary. We have to weigh and design. But no matter what, remember that when the Activity is destroyed, the reference to the Activity is removed in the singleton. Let's discuss below: What happens if it is an inner class? For example, we have a very short non-static Handler in an Activity.

Although it looks short, as long as it is alive, the Activity containing it will be alive. If you don't believe me, try it in a VM. This is another case of memory leak: Handler inside Activity.

public class MainActivity extends Activity { //...  Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 
//...  handler = new Handler() { @Override public void handleMessage(Message msg) {  
              }  
  }  
}
Copy after login

Handler is a very common and useful class, asynchronous, thread-safe, etc. What would happen if we had code like the following? handler.postDeslayed , assuming the delay time is hours... what does this mean? This means that as long as the handler's message has not been processed, it will always live, and the Activity containing it will also live. Let's find a way to fix it. The solution is WeakReference, which is the so-called weak reference. The garbage collector will ignore weak references when recycling, so the Activity containing them will be cleaned up normally. The approximate code is as follows:

In summary: We have an internal class, just like Handler. Internal non-static classes cannot survive independently of the class they belong to. In Android, it is usually Activity. So, take a look at the inner classes in your code and make sure they don't leak memory.

It is better to use static inner classes than non-static inner classes. The difference is that static inner classes do not depend on the class they belong to, they have different life cycles. I often see memory leaks caused by similar reasons.

How to avoid Activity leaks?

移除掉所有的静态引用。
考虑用 EventBus 来解耦 Listener。
记着在不需要的时候,解除 Listener 的绑定。
尽量用静态内部类。
做 Code Review。个人经验:Code Review 能很早的发现内存泄漏。
了解你程序的结构。
用类似 MAT,Eclipse Analyzer,LeakCanary 这样的工具分析内存。
在 Callback 里打印 Log。
Copy after login

Swipe

实现流畅滑动的技巧:UI 线程只用作 UI 渲染。这一条真谛能够解决 99% 的滑动卡顿问题。不要在 UI 线程做下面的事情:

载入图片
网络请求
解析 JSON 读取数据库
Copy after login

做这些操作是很慢的,像图片,网络,JSON考虑用现成的库,有很多社区提供的解决方案,数据库考虑下用 Loader,支持批量更新和载入。

图片

图片相关的库有很多,比如 Glide, Picasso, Fresco。你可以自己去了解下他们之间的区别,以帮助自己在特定场景下做出取舍。

内存

Bitmap 操作是很需要技巧的,图片一般比较大,而且系统对最大内存又有限制和要求。在我面对 4.0 之前的系统的时候,我简直要崩溃了。内存管理也很需要技巧。有的时候需要放到文件里,有的时候需要放到内存里,别忘了,我们还有一个很有用的工具:LRUCache。

网络

首先,Java 的网络请求确实是 Android 的一个阻碍。很多 Java.net 的 API 都是阻断执行的,切记不可在 UI 线程执行网络请求。在线程里执行或者直接使用第三方库吧。

异步 HTTP 其实也挺麻烦的,4.4 起 OkHttp 就成了 Android 代码的一部分了,然而… 如果你需要最新版本的 OkHttp ,可以考虑自己引入。另外有个不错的库叫: Volley,也可以试试 Square 的 Retrofit。这些都能让你的网络请求变得更友好。

大 JSON

在 UI 线程,也不做解析 Json 的事情,因为这是一个很耗时的事情。试着用 Google 的 GSON 来做反序列化的操作。

对于巨大的 JSON 解析,建议用更快的 Jackson 以及 ig-json-parser,这两个工具在 JSON 的解析上做的非常漂亮。从公司的反馈结果来看 ig-json-parser 的效率是最高的。

Looper.myLooper() == Looper.getMainLooper() 是可以帮助你确定你是否在主线程的代码。

如何优化滑动速度?

UI 线程只做 UI 更新。
理解并发 API。
开始使用优秀的第三方库。
使用 Loader 加载数据库数据
Copy after login

之所以要用第三方库,是因为你自己去完善一个复杂功能是需要花时间的。如果你打算专注在自己的功能性的 App 上,那么用库吧。

并发 APIs

如何让 App 快速响应请求是个很重要。开发者们,甚至包括我,经常忘记 Service 的方法是在 UI 线程执行的。请考虑使用 IntentService,AsyncTask,Executors,Handler 和 Loopers。

我们来盘点下这些的区别:

IntentService

我在之前的公司,我用 IntentService 来执行上传功能。IntentService 是一个单线程,一次一个任务的工作流。我们没有很复杂的任务系统。如果你有大型复杂的任务,而且这个任务不需要跟 UI 打交道,那么考虑用 IntentService 吧。

AsyncTask

如果你的任务需要更新 UI,那么考虑用 AsyncTask 吧,AsyncTask 虽然相对容易,但是有些坑得留意。当你旋转手机的时候,Activity 会被关闭,然后重启。不然可能造成内存泄露。

Executor Framework

这是 Java 6 自带的并发方案。默认是存在一个由系统管理的线程池,你可以通过 callback,future 来控制和管理。这根 MapRedues 发难有点像,面对复杂的任务,你希望能够把他们拆分交给多个线程来处理。Executor 的框架就很能胜任这种场景。

如何适应并发APIs?

学会和理解 API,懂得权衡
确保找到了问题的正确解决方案
了解问题真实所在
重构代码
Copy after login

Deprecation

我们肯定都知道,最好能够避免使用废弃的 API。比如以下的例子:

不要通过反射来调用私有 API。
不要再 NDK 和 C 语言层调用私有 Native 方法。
不要轻易调用 Runtime.exec 指令完成进程通讯功能。
adb shell am 做进程通讯并不好。
Copy after login

废弃的意思是这些 API 将会被移除,通常在正式版发布 1,2天左右,你的 App 就不会工作了。更糟糕的情况是,如果你的 App 依赖了一些库,而这些库哟改了废弃的 Api 或者工具。那可就惨了,如果一旦作者没有更新…你懂得。

不要用废弃 Api 的另一个原因是性能问题和安全问题。

如何避免废弃 Api:

使用正确的 API。
重构依赖。
不要滥用系统。
更新依赖和工具。
越新的通常越好。
Copy after login

用 Toolbar 而非 ActionBar,在需要动画的时候用 RecyclerView,因为它专门为动画做过优化。同时 Android M 里移除了 Apache Http Connection。请使用 HttpURLConnection,它拥有更简单的 API,更小的体积,默认的压缩功能,更好的 Response 缓存,等等其他很赞的功能。

架构

架构中的 Bug 总是最为烦人。想要避免这种问题,学习下 App 组件的生命周期。比如什么是 Activity 的 Flag?什么是 Fragment?什么事 stated fragment?什么是 task?读读文档,尝试下用回调的 log 搞清楚这些概念。

时常有人问我:“Picasso 和 Glide 哪个更好?我改用 Volley 还是 OkHttp?”,这种问题根本没有 100% 正确的答案。不过,当我在选择一个库的时候,我会用下面的 Checklist 来决策:

确保它能够解决你的问题。
确保它和当前所有的依赖能正常工作。
检查依赖
留意一下依赖的版本冲突
了解维护情况和成本
Copy after login

总的来说,提及架构和设计,最好的方法就是让你的程序最快响应。确保用户能够快速理解你的 App,并且拥有良好体验。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template