Lucas
Lucas

Reputation: 3591

Android application lifecycle clarification

Can anybody confirm the following regarding the android application lifecycle?

1) When application is in foreground the memory will contain instance of the Application object, instances of all activities (not killed) and all the object references that are referenced from one of the root's (haven't been garbage collected)

2) When application goes to background, at some point the Android Framework can: a) Kill the whole process given to the purpose of the application which will essentialy erase all the objects from the memory b) Kill ONLY (so essentialy no other object reference will be deleted) the activities (by finishing them and in essence any fragments as well) saving their states and creating the Activities Stack and leaving anything else (Application object, any other static objects, references that are reachable from any of the roots).

I'm mostly interested in 2b, but I would appriciate confirmation on all of these points as I'm trying to grasp mentaly the whole concept from start to finish.

Upvotes: 15

Views: 541

Answers (6)

Lucas
Lucas

Reputation: 3591

Ok, during my search quest in recent weeks I was able to get some more information and now I can answer my own (and hopefully for others) questions:

1) Correct

2a) Correct

2b) False. The android framework, if in need of memory or if due to some other reason it has to "kill/reduce" the application it can do so only by killing the whole process that is dedicated to that application. The android framework can neighter kill chosen activity(ies) or kill all activities but leave all other objects alive (like Application object, singletons etc.)

Upvotes: 0

Viral Patel
Viral Patel

Reputation: 33438

1) When application is in foreground the memory will contain instance of the Application object, instances of all activities (not killed) and all the object references that are referenced from one of the root's (haven't been garbage collected)

> There will only ever be a few such processes in the system, and these
> will only be killed as a last resort if memory is so low that not even
> these processes can continue to run. Generally, at this point, the
> device has reached a memory paging state, so this action is required
> in order to keep the user interface responsive.

2) When application goes to background, at some point the Android Framework can:

.

a) Kill the whole process given to the purpose of the application which will essentialy erase all the objects from the memory

> These processes have no direct impact on the user experience. Provided
> they implement their Activity life-cycle correctly (see Activity for
> more details), the system can kill such processes at any time to
> reclaim memory for one of the three previous processes types. Usually
> there are many of these processes running, so they are kept in an LRU
> list to ensure the process that was most recently seen by the user is
> the last to be killed when running low on memory.

b) Kill ONLY (so essentialy no other object reference will be deleted) the activities (by finishing them and in essence any fragments as well) saving their states and creating the Activities Stack and leaving anything else (Application object, any other static objects, references that are reachable from any of the roots).

Partially as in point 2.a.'s explanation

> Usually there are many of these processes running, so they are kept in an LRU
> list to ensure the process that was most recently seen by the user is
> the last to be killed when running low on memory.

Source - developer.android.com

Upvotes: 4

Developer Hayat
Developer Hayat

Reputation: 1

Well this thing depends upon how Android OS operates. Android Device is an embedded System but works almost same as a PC and when I say Android as an OS, it will definetely have all the features of an OS. The thing which you are pointing upon is the Memory Management and Scheduling feature of Android OS. MMU(Memory Management Unit) logically give preference to currently executing task i.e ur launcher or any other Application which u r working upon. Two things I want to answer which can help u bit more:

  1. Views (whether xml generated or Javacode generated, they are dynamically generated.).
  2. Android OS runs all apps as a process with sub processes(Activities) on a Dalvik Virtual Machine.
  3. All your Activities before they are created they are null, when they are created then their instance is generated. Upon opening them again their saved instance is again viewd(Concept of Singleton Design Pattern).

So let me tell you that I don't think that both of the options are right. What I beleive is following: 1. View will always be generated dynamically. 2. Instance will be saved in Memory. 3. On background going of Application the whole process with instances available will be their in Memory.

Upvotes: 0

Geeky Singh
Geeky Singh

Reputation: 1013

There is no typical life-cycle for application exists. Application object lives in memory until it is not being killed either by Android itself or by manually by user.

For the above points, here are your answers:

1) This is true.

2) a) That is also true. 2) b) When application goes background, you can only save the data of the current activity that was in foreground. Also, when you kill the application it self (by removing it from recent list), all the activities in the app stack and their saved data (not persistent data) got killed as application is the base for all the activities.

Upvotes: 4

Jim Baca
Jim Baca

Reputation: 6122

Yes you are mostly correct about 2b).

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.

However there are instances where onSaveInstantSate isn't called:

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

Android Docs Source

You can request android to always destroy activities on background by enabling the following developer option. If you are debugging your app you should be able to step through the life cycle methods and see what is happening.

Settings -> Developer options -> Apps -> Don't keep activities

Upvotes: 4

CodingRat
CodingRat

Reputation: 1944

If you are looking for official confirmation then better ask Google only :).

but i feel after reading this you will get a better understanding of these concept.

Android memory management

android process lifecycle

Answer for 1st question: yes confirm using DDMS.

answer for 2a question: yes OS can kill process any point of time when needed memory for other process which will result into killing all object related to process.

answer for 2b questiong:

From official documentation

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.

Upvotes: 4

Related Questions