Android Zygote & Dalvik VM
Zygote & Dalvik
1. What is zygote and dalvik?
Zygote
Zygote is a daemon process that provides efficient memory usage and less time overhead when Android runs multiple application. Zygote is the parent of all application processes.
Dalvik
Dalvik is a VM process that runs each application. All Dalvik VM are forked from zygote for minimizing overhead purpose.
2.1 Zygote
Zygote process
The order of the Zygote’s initialization process is :
- init starts zygote using /system/bin/app_process
- Zygote cold boot Dalvik
- Register zygote socket
- Pre-load Classes and Resources
- Zygote fork() system_server -> starts all system services
- Zygote listens a command via socket
- Zygote spawns a new process as requested
1. init starts zygote using /system/bin/app_process
Init process is the first process that runs on Android startup, and it runs init.rc script to initialize the system and start daemon services. Zygote VM process is one of the daemon processes started by init. [More detailed Android startup process is here : TODO]
Below is the part of init.rc code to start zygote.
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main socket zygote stream 660 root system
As you can see, init process runs /system/bin/app_process to start zygote, and notice socket command there.
2. Zygote cold boot Dalvik
Before zygote goes into main(), it starts up Dalvik VM to warm up the virtual machine for a new process.
3. Zygote registers a socket
4. Pre-load Classes and Resources
When zygote startup, it preloads classes keeps them in memory to be re-used by forked application, and preloads resources to be built into framework-res.apk.
5. Zygote fork() system_server
Then, zygote fork() system_service. All the system services are started by system_service process including Naive System Service and Java System Service to create Java environment.
6. Zygote listens a command via socket
After zygote sets up the initialization, it will listen to the socket and infinitely loop until there’s a new command for a process. When zygote receives a new command, it will fork a new process.
2.2 Motivations for dalvik implementation
As you can see, Zygote and Dalvik are closely related. To explain what they are, it would be better to explain what made Google to make Dalvik VM.
Several factors motivated Google to make dalvik :
- Security
- Limited Resource
- J2ME’s problems
- Licensing issue
- Open Source
1. Security
Most of times, mobile devices contain significantly sensitive personal information including, phone numbers, pictures, location, and more. Therefore, Android needed to protect applications from other threatening applications to secure the information. In response to this concern, Dalvik was designed to take advantage of Linux Kernel’s strong security model by having each process running on a independent virtual machine. This sandboxes processes from each other to create a strong barrier between applications. Furthermore, separating the address space for each application gives more reliability by preventing to close all running processes because of one faulting process.
2. Limited Resource
Then, whey don’t they just use other existing VMs?
It is because desktop and mobile environments are very different. Therefore, running a desktop application on mobile devices will cause various problems and inefficient. Furthermore, there are more factors to consider in mobile environment : battery power, passive cooling, and smaller form.
Mobile devices need to conserve battery energy aggressively because they have significantly less battery capacity with no consistent power supply. The smaller form of mobile devices led to have relatively slow ARM processor, and using the processor with high pressure will almost melt the mobile devices. Therefore, Google had to make a VM that can be used on mobile environment.
.dex file [TODO]
Why use from delvik to ART? [TODO]
Resources:
Detailed Explanation about Zygote including its motivation, performance, Implementation
Paper of Analysis on Process Code schedule of Android Dalvik Virtual Machine
Simple and clear explanation of zygote and dalvik
Paper of The Dalvik virtual Machine Architecture (Design overview, .dex file)
Well explained in stackOverflow