Android 内存管理粒度 - Activity 还是进程?
Android memory management granularity - Activity or Process?
关于 Android 内存不足时发生的情况以及 OS 回收内存所采取的步骤,我看到了不一致的文档和讨论。更具体地说,Android 是在 activity/fragment 的粒度还是整个过程中杀死?
例如,如果 Activity B 在 Activity A 之前启动(并且两个活动都是同一个 app/process 的一部分),Activity A 可以吗?当 Activity B 在前台并且用户正在与 Activity B 交互时被 OS 杀死(假设:屏幕保持打开状态,当前应用程序保持在前台,没有发生方向变化) ?
2011 年的 SO answer(作者是 Google Android 团队的 Dianne Hackborn)表明 Android 在进程的粒度上杀死,而不是 activity.
在 Recreating an Activity 的 Android 开发者页面上,它说:
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.
注意歧义:"the system must shut down background PROCESSES"。
在 onSaveInstanceState 的 Android 开发者页面上,它说:
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
阅读了这些以及许多其他文档页面和在线讨论后,尚不清楚正确答案是什么。
关于片段我也有同样的问题:是否可以在不杀死整个进程的情况下因内存不足而杀死后台片段?
我会在 Android 指南和文档方面犯错(尽管,如果它们在代码文档和 SO 答案中更清楚,那就太好了)。来自 http://developer.android.com/guide/components/tasks-and-back-stack.html :
When the system stops one of your activities (such as when a new activity starts or the task moves to the background), the system might destroy that activity completely if it needs to recover system memory. When this happens, information about the activity state is lost. If this happens, the system still knows that the activity has a place in the back stack, but when the activity is brought to the top of the stack the system must recreate it (rather than resume it). In order to avoid losing the user's work, you should proactively retain it by implementing the onSaveInstanceState() callback methods in your activity.
内存管理发生在两个不同的级别:通过垃圾收集(回收未引用的对象)和进程级别,如 this Android blog post 中所述。 没有只杀一个的概念activity(记住:Android是基于Linux而Linux没有概念活动或组件,只是流程)。
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.
这仍然是正确的。
On the Android Developer pages on Recreating an Activity, it says...
没错,它说的'background processes'就是上面博客和the documentation中提到的进程类别。这是指以前存在但不再属于当前 foreground/visible 流程的活动。
On the Android Developer pages for onSaveInstanceState, it says:
是的,他们正在讨论您从另一个进程启动 Activity 的情况(当您使用 implicit intents 时很可能)。这段时间你的进程不是前台进程,前台activity+前台服务组合太多肯定有可能被kill掉。
I also have the same question regarding fragments: Can a backgrounded fragment be killed due to low memory, without its entire process being killed?
不行,fragments内存不足无法杀掉
它是 "Process" 而不是 "Android Activity"。部分混淆在于 "ActivityManager" 的命名,它进行部分内存分析,还管理 Android-Activity 接口。然而,真正负责根据 ActivityManager 和其他系统接口提供的信息停止进程的是 LMK(低内存杀手)。
的 "The relationship between oom_adj and upper Process Importance" 部分找到了对此的简要分析
关于 Android 内存不足时发生的情况以及 OS 回收内存所采取的步骤,我看到了不一致的文档和讨论。更具体地说,Android 是在 activity/fragment 的粒度还是整个过程中杀死?
例如,如果 Activity B 在 Activity A 之前启动(并且两个活动都是同一个 app/process 的一部分),Activity A 可以吗?当 Activity B 在前台并且用户正在与 Activity B 交互时被 OS 杀死(假设:屏幕保持打开状态,当前应用程序保持在前台,没有发生方向变化) ?
2011 年的 SO answer(作者是 Google Android 团队的 Dianne Hackborn)表明 Android 在进程的粒度上杀死,而不是 activity.
在 Recreating an Activity 的 Android 开发者页面上,它说:
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.
注意歧义:"the system must shut down background PROCESSES"。
在 onSaveInstanceState 的 Android 开发者页面上,它说:
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
阅读了这些以及许多其他文档页面和在线讨论后,尚不清楚正确答案是什么。
关于片段我也有同样的问题:是否可以在不杀死整个进程的情况下因内存不足而杀死后台片段?
我会在 Android 指南和文档方面犯错(尽管,如果它们在代码文档和 SO 答案中更清楚,那就太好了)。来自 http://developer.android.com/guide/components/tasks-and-back-stack.html :
When the system stops one of your activities (such as when a new activity starts or the task moves to the background), the system might destroy that activity completely if it needs to recover system memory. When this happens, information about the activity state is lost. If this happens, the system still knows that the activity has a place in the back stack, but when the activity is brought to the top of the stack the system must recreate it (rather than resume it). In order to avoid losing the user's work, you should proactively retain it by implementing the onSaveInstanceState() callback methods in your activity.
内存管理发生在两个不同的级别:通过垃圾收集(回收未引用的对象)和进程级别,如 this Android blog post 中所述。 没有只杀一个的概念activity(记住:Android是基于Linux而Linux没有概念活动或组件,只是流程)。
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.
这仍然是正确的。
On the Android Developer pages on Recreating an Activity, it says...
没错,它说的'background processes'就是上面博客和the documentation中提到的进程类别。这是指以前存在但不再属于当前 foreground/visible 流程的活动。
On the Android Developer pages for onSaveInstanceState, it says:
是的,他们正在讨论您从另一个进程启动 Activity 的情况(当您使用 implicit intents 时很可能)。这段时间你的进程不是前台进程,前台activity+前台服务组合太多肯定有可能被kill掉。
I also have the same question regarding fragments: Can a backgrounded fragment be killed due to low memory, without its entire process being killed?
不行,fragments内存不足无法杀掉
它是 "Process" 而不是 "Android Activity"。部分混淆在于 "ActivityManager" 的命名,它进行部分内存分析,还管理 Android-Activity 接口。然而,真正负责根据 ActivityManager 和其他系统接口提供的信息停止进程的是 LMK(低内存杀手)。
的 "The relationship between oom_adj and upper Process Importance" 部分找到了对此的简要分析