Android destroying activities, killing processes
嗨,我想知道安卓是如何管理内存的,我在任何地方都找不到准确的答案。假设我有一个应用程序,在当前活动堆栈上有5个活动(4个停止,1个恢复),没有连接的服务。我按"主页"按钮以停止所有活动。我启动了一些消耗内存的应用程序,整个设备内存开始变低。问题是
…我的申请会怎么样?
更新:
在问这个问题之前,我已经看过几次活动生命周期,但它没有回答我的问题。我做了一些测试,我有一些答案。"DDMS中的"停止进程"是测试的线索。
我还没有测试问题1的答案,但正如指南所说:
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.
似乎一个或多个活动可以被轻而易举地销毁(使用OnDestroy方法),而不会破坏进程。当你回到他们身边时,你只需要得到(oncreate+bundle)。
问题2答案:
对。一般来说,系统会杀死整个过程,这意味着包括活动和静态字段在内的所有数据都会被销毁。这做得不好-对于任何暂停/停止的活动,您都不会获得OnDestroy或Finialize()。这就是为什么在OnPause方法之前调用SaveInstanceState()。onpause基本上是最后一个应该保存某些内容的方法,因为在此方法之后,您将永远无法看到onstop或ondestroy。系统可以终止这个过程,摧毁你所有的物体,不管它们持有什么,不管它们正在做什么。
问题3答案:
当你回到一个被杀的应用程序时会发生什么?
- 在Android 2.2之前-应用程序将从Begining开始,启动程序活动。
- 从2.2开始-系统将恢复以前的应用程序状态。这是什么意思?这意味着最后一个可见的活动将被重新创建(onCreate+bundle)。活动堆栈将发生什么?堆栈很好,但上面的所有活动都已停止。当您使用"后退"按钮返回时,它们中的每一个都将被重新创建(oncreate+bundle)。还有一件事:
Normally, the system clears a task (removes all activities from the
stack above the root activity) in certain situations when the user
re-selects that task from the home screen. Typically, this is done if
the user hasn't visited the task for a certain amount of time, such as
30 minutes.
结论?
就是这样…希望我能帮上忙。
首先,请看一下:
onPause() Called when the system is about to start resuming a
previous activity. This is typically used to commit unsaved changes to
persistent data, stop animations and other things that may be
consuming CPU, etc. Implementations of this method must be very quick
because the next activity will not be resumed until this method
returns. Followed by either onResume() if the activity returns back to
the front, or onStop() if it becomes invisible to the user.onStop() Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this
one. This may happen either because a new activity is being started,
an existing one is being brought in front of this one, or this one is
being destroyed. Followed by either onRestart() if this activity is
coming back to interact with the user, or onDestroy() if this activity
is going away.
所以,当你按下设备上的"主页"按钮时,你当前的前台活动被放在
根据谷歌的文件:
- 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.
对于流程生命周期:
Process Lifecycle 3. A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so
the system may safely kill its process to reclaim memory for other
foreground or visible processes. If its process needs to be killed,
when the user navigates back to the activity (making it visible on the
screen again), its onCreate(Bundle) method will be called with the
savedInstanceState it had previously supplied in
onSaveInstanceState(Bundle) so that it can restart itself in the same
state as the user last left it.
以上所有报价来自:Android开发者参考:活动
当您启动一些内存消耗应用程序时,系统可以破坏非acitve活动并回收内存。您可以在您的活动中实现像:
更新
以下是我在这里发现的一些观点:
Stopped state
When an activity is not visible, but still in memory, we say it’s in a
stopped state. Stopped activity could be brought back to the front to
become a Running activity again. Or, it could be destroyed and removed
from memory.The system keeps activities around in a stopped state because it is
likely that the user will still want to get back to those activities
some time soon, and restarting a stopped activity is far cheaper than
starting an activity from scratch. That is because we already have all
the objects loaded in memory and simply have to bring it all up to the
foreground.Stopped activities can be removed from memory at any point.
Can system destroy only one or some of my activities to recover
memory?
对。当需要内存时,Android会终止后台运行的活动。杀死一个或全部可能取决于某些条件。例如,暂停或停止的实例会使Android终止活动或进程本身。在"活动生命周期"下,您可以得到以下几点。我建议你把那一页彻底翻一遍。它一定会消除你的疑虑。
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.
Will system kill the whole process of my application? Will all
activities be nicely destroyed?
活动属于个人,而过程属于一组活动。再看上面的第三点,它终止了前面提到的过程。
What will happen when I get back to my application when it was totally
killed?
类似于重启。第三点会给你一些答案,比如
在这里了解更多关于记忆的信息。
编辑:应用程序中的所有活动都在单个进程中运行。因此,当一个进程被终止时,所有活动(无论是5还是10)都将被终止,即重新启动。重新启动将导致应用程序从头开始,没有保存状态。