android - 制作 Android 应用程序时是否有需要注意的内存管理技巧?

标签 android android-layout android-memory

我最近在接受采访时被问到一些关于 Android 的问题。我搜索了其中一些,但找不到合适的资源和答案。所以想在这里和大家分享一下。

  1. 在 Android 中使用的首选布局是什么(为了更好的内存消耗等)? 我对此没有答案,面试官告诉我这是相对布局。这是真的 ?有什么解释吗?

  2. 请告诉我您为了更好地消耗内存而采取的一些做法? 我看了here但似乎还有其他东西。因为面试官提到一些静态变量相关的东西比较好。

  3. 如果 Android 需要内存,它会终止服务或 Activity 吗?优先事项。我也没有找到任何人讨论这个。面试官说有些关于服务的优先级(??)比 Activity 高,所以 Activity 是将被杀死的组件。这是真的 ?任何进一步的资源或解释?

请分享您知道的有关此问题的任何知识或资源。

最佳答案

一一解答:

编号1

听起来不对。这样说是错误的RelativeLayout 总是比任何其他布局都快。使布局“快”或“慢”的是计算所有子元素的位置和大小所需的时间。所以当你只显示 15 行 TextView 时,一个在另一个下面,一个LinearLayout肯定会更快(并且使用起来不那么麻烦)。

通常,我建议使用最适合您情况的布局。

编号2

首选静态变量的“优势”是它们只被初始化一次(因此在内存中)。但这与其说是性能决策,不如说是设计决策。

您应该避免在内存中使用巨大的嵌套集合(例如 List<List<HashMap<?,?>> ),但这应该是常识。对象创建的问题是,如果您创建许多对象并且不保留对它们的任何引用,它们将被垃圾收集。这将为您的应用程序增加运行时开销。

编号3

这既对又错。服务可以以不同的优先级启动。但是,在您的应用程序使用的任何内容(无论是服务还是 Activity )被终止之前,后台应用程序及其资源将被释放。

对于服务,文档给出了多个提示:

The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it. When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher of the following possibilities: [Full List]

关于 Activity ,the following is listed :

An activity has essentially four states:

  • If an activity in the foreground of the screen (at the top of the stack), it is active or running.

  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

因此,对于 Activity 而言,它取决于当前状态,它被杀死的可能性有多大。

结论

“M. A. Jackson”关于优化的引述:

We follow two rules in the matter of optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet - that is, not until you have a perfectly clear and unoptimized solution.

因为“太慢”而不使用特定平台功能通常不是一个好主意。 Google 和 Oracle 非常注意他们的标准库是 尽可能优化。这种事情就让专家操心吧。

关于android - 制作 Android 应用程序时是否有需要注意的内存管理技巧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11812437/

相关文章:

android - 长按时 ActionBarImplICS(使用应用程序兼容)出现 NullPointerException

android animatorset内存管理,从imageview中释放内存

不同版本之间的Android应用服务内存使用差异

Android:如果文本字段很长,图像不显示

android - 如何包装textview自动调整大小的内容

android - OutOfMemoryError 尽管 vm 有足够的空闲内存

java - 设置多个 TextView Android

android - 如何重构一个包含很多监听器内部类的类?

android - 我的应用程序在 Android 市场上无法搜索

Android 设备在 Ubuntu 13.04 中未在线显示