问题描述
我看到关于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)?
此 2011年的答案"(由Google的Android团队的Dianne Hackborn撰写)表明,Android的杀伤力在于进程的粒度,而不是活动的粒度.
On the Android Developer pages on Recreating an Activity, it says:
注意含糊不清:系统必须关闭后台过程".
Notice the ambiguity: "the system must shut down background PROCESSES".
在的Android开发人员页面上onSaveInstanceState ,它表示:
在阅读了这些文档以及许多其他文档页面并进行了在线讨论之后,尚不清楚正确的答案是什么.
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?
推荐答案
内存管理发生在两个不同的级别:通过垃圾回收(回收未引用的对象)和进程级别,如此Android博客帖子.没有杀死单个活动的概念(请记住:Android基于Linux,Linux没有活动或组件的概念,而只是进程).
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).
这仍然是正确的.
是的,它提到的后台进程"正是上述博客和文档.这是指以前存在的活动,但不再是当前前景/可见进程的一部分.
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.
是的,他们正在讨论您从另一个进程启动活动的情况(当您使用隐式意图).在这段时间内,您的过程不是前台进程,因此,如果前台活动和前台服务的组合过多,则肯定有可能被杀死.
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.
不,由于内存不足,无法杀死片段.
No, fragments cannot be killed due to low memory.
这篇关于Android内存管理粒度-活动还是流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!