Android内存管理粒度-活动还是流程? [英] Android memory management granularity - Activity or Process?

查看:47
本文介绍了Android内存管理粒度-活动还是流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到关于Android内存不足以及OS收回内存所采取的步骤时会发生什么的不一致的文档和讨论.更具体地说,Android会杀死活动/片段或整个过程的粒度吗?

I am seeing inconsistent documentation and discussion regarding what happens when Android is low on memory and the steps the OS takes to re-claim memory. More specifically, does Android kill at the granularity of activity/fragment, or entire process?

例如,如果在活动A的前面启动了活动B(并且两个活动都是同一应用程序/进程的一部分),那么当活动B在前台并且用户进行交互时,活动A可以被操作系统杀死吗?使用活动B(假设:屏幕保持打开状态,当前应用程序保持在前景状态,方向没有发生变化)?

For example, if Activity B is launched in front of Activity A (and both activities are part of the same app/process), can Activity A be killed by the OS while Activity B is in the foreground and the user is interacting with Activity B (assume: screen remains on, current app remains in the foreground, no orientation change occurs)?

This SO answer from 2011 (by Dianne Hackborn on the Android team at Google) suggests that Android kills at the granularity of a process, NOT an activity.

重新创建活动的Android开发人员页面上,它说:

On the Android Developer pages on Recreating an Activity, it says:

如果当前活动已停止并且很长时间未使用,或者前台活动需要更多资源,则系统也可能破坏您的活动,因此系统必须关闭后台进程以恢复内存.

The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

注意含糊不清:系统必须关闭后台过程".

Notice the ambiguity: "the system must shut down background PROCESSES".

的Android开发人员页面上onSaveInstanceState ,它表示:

例如,如果活动B在活动A之前启动,并且在某个时候活动A被杀死以回收资源,则活动A将有机会通过此方法保存其用户界面的当前状态

For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method

在阅读了这些文档以及许多其他文档页面并进行了在线讨论之后,尚不清楚正确的答案是什么.

After reading through these and many other doc pages and online discussion, it is not clear what the correct answer is.

对于片段,我也有同样的问题:背景片段是否可以由于内存不足而被杀死,而不会杀死整个过程?

I also have the same question regarding fragments: Can a backgrounded fragment be killed due to low memory, without its entire process being killed?

推荐答案

内存管理发生在两个不同的级别:通过垃圾回收(回收未引用的对象)和进程级别,如

Memory management happens at two distinct levels: through garbage collection (recycling unreferenced objects) and at the process level, as explained in this Android blog post. There is no concept of killing just a single activity (remember: Android is based on Linux and Linux has no concept of activities or components, just processes).

2011年的SO答案(由Google的Android团队的Dianne Hackborn提出)表明,Android的杀伤力在于进程的粒度,而不是活动的粒度.

This SO answer from 2011 (by Dianne Hackborn on the Android team at Google) suggests that Android kills at the granularity of a process, NOT an activity.

这仍然是正确的.

在重新创建活动"的Android开发人员页面上,显示为...

On the Android Developer pages on Recreating an Activity, it says...

是的,它提到的后台进程"正是上述博客和文档.这是指以前存在的活动,但不再是当前前景/可见进程的一部分.

Yes, the 'background processes' it mentions is exactly the process category mentioned in the above blog and the documentation. This refers to activities that previously existed, but are no longer part of the current foreground/visible processes.

在onSaveInstanceState的Android开发人员页面上,它显示:

On the Android Developer pages for onSaveInstanceState, it says:

是的,他们正在讨论您从另一个进程启动活动的情况(当您使用隐式意图).在这段时间内,您的过程不是前台进程,因此,如果前台活动和前台服务的组合过多,则肯定有可能被杀死.

Yes, they are discussing the case where you launch an Activity from another process (as is likely when you are using implicit intents). During this time, your process is not the foreground process and therefore is certainly possible to be killed if the combination of foreground activity+foreground services is too much.

对于片段,我也有同样的问题:背景片段是否可以由于内存不足而被杀死,而不会杀死整个过程?

I also have the same question regarding fragments: Can a backgrounded fragment be killed due to low memory, without its entire process being killed?

不,由于内存不足,无法杀死片段.

No, fragments cannot be killed due to low memory.

这篇关于Android内存管理粒度-活动还是流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆