Android OS boot process explained with focus on Zygote
You just started with Android development and went through setup new project wizard in Android Studio, pressed that glorious green play button and deployed app to the emulator or device. Logcat window pops up and there is a bunch of logs, but every time you look closely you see Zygote did this, Zygote did that. So what is Zygote? As presented by Wiki the Zygote is
"the initial cell formed when a new organism is produced"
To get to the technical explanation it's best to start from the beginning of boot process. As you may know, Android is based on Linux kernel, and the device boot process usually goes like this:
Bootrom is a small piece of write-protected flash rom memory embedded inside the processor chip. It contains the very first code that is executed by the processor when it powers up or resets.
BootLoader is started by the bootrom, it’s job is to execute any specific setup before starting the Kernel, which literary means to copy os files to working memory.
Kernel will start setup cache, protected memory, scheduling and loads drivers. When kernel finishes system setup first thing it does is look for “init” in system files and launches root process or the first process of the system.
init is a root process, it has two responsibilities, mount directories like /sys, /dev, /proc and run init.rc script that starts among other things native daemons like Service Manager, Media Server etc..
Android runtime is started by init root process with app_process command which tells it to start Art or Dalvik process virtual machine and to call Zygote's main() function.
Art/Dalvik are process virtual machines. Dalvik is used on devices below Lollipop where's it is replaced by Art. The biggest difference between them is that Dalvik uses JIT (just in time) and Art uses AOT (ahead of time) compilation.
Zygote is a special Android OS process that enables shared code across Dalvik/Art VM in contrast with Java VM where each instance has its own copy of core library class files and heap objects.
Efficient and fast app launch is achieved due to the fact that Zygote starts by preloading all classes and resources which an app may potentially need at runtime into the system’s memory. It then listens for connections on its socket for requests to start new apps. When it gets a request to start an app, it forks itself and launches the new app. It serves as a parent to all Android apps.
This forking feature comes from Linux kernel implementation of copy-on-write resource management technique. Forking involves creating a new process that is an exact copy of the parent process. It doesn’t actually copy anything, instead it maps pages of the new process over to those of the parent process and makes copies only when the new process writes to a page. All processes started from Zygote use it's own copy and only one copy of the system classes and the resources that are already loaded in system memory.
Zygote is designed not only to respond to fork requests by new apps, but there is one process that the Zygote actually starts explicitly and that is the System Server process.
System Server is the first process started by the Zygote. After it starts it lives on as an entirely separate process from its parent. It starts initializing each system service it houses and registering them with the previously started Service Manager. It also starts the Activity Manager.
Activity Manager is responsible for a new Activity thread process creation, maintenance of the Activity lifecycle and managing the Activity stack. By the end of it's startup it also executes an Intent to start a Home Launcher over which it receives onClick events to start new apps from icons on the home screen.
Useful links :
Thanks for reading. Please share or recommend this article if you found it helpful.