In a Linux, each user has its own process and a unique ID. Every user has access to private resources proctected by permission and any user (except the super user) can access another user’s private resource. In Android, the same thing happens, but instead of users we have applications.
By default, applications and process have a one-to-one relationship, but if required, it is possible for an application to run in several process, or for serveral applications to run in the same process.
Starting a process is slow and the comunication between process is slow too. A process allocates memory for its own substantial resources. So, an application that uses more than one process to work need to lead in consideration the loss of performance that will happen.
The Android try to shorten the statup time of the applications, because start the runtime and the Linux process is not light. It does it throught the Zygote, that is a process that has the entire set of core libraries preloaded. Every new process created is a fork from Zygote withou copying the core libraries, because the Zygote share it with all process.
When any application’s component is initiated for execution, the application is started. So, any component can be the entry-point for the application starting.
- A component is called
- Start Linux process
- Create a runtime
- Create Application instance
- Create the component
Application instance is the first component to be instantiated in a process and the last to be destroyed.
The runtime avoids to destroy all resources of an application when it’s killed because a user may request the same application at any later time. The runtime only destroys the resources when a number of live applications leads to an actual shortage of resources across the system and the system needs to kill the entire process. So, the application is started when one of its components is called, but it’s not completly terminated even when all of its components have been destroyed.
The runtime is responsible for make the lifecycle of the application happens completly, but the Linux process can be killed before the runtime had a chance to call every lifecycle method of the components. It can happen when the system needs located resources. For example, the method onTerminate() of the Application instance cannot be called by the runtime during the application killing because the Linux process was killed by the system before.