Why do Android apps open so fast?

If you’re an Android system developer, you might know what happens under the hood when a user taps on an app icon. But if you’re not, or if you’re just an Android app developer, it’s worth discovering how the Android system works to help you build better apps and improve performance.

When a user taps on an app icon, magic happens thanks to a process called Zygote. Zygote uses a brilliant concept that came from earlier Unix-like operating systems called Copy-on-Write (COW).

Zygote: The Parent of All Java Processes

According to Wikipedia’s definition:

Copy-on-write (COW), also called implicit sharing or shadowing, is a resource-management technique used in programming to manage shared data efficiently. Instead of copying data right away when multiple programs use it, the same data is shared between programs until one tries to modify it.

When Android boots up, the operating system follows this sequence:

  1. init The very first process (PID 1), written in C/C++
  2. zygote The first Java runtime process that loads the Android framework
  3. system_server The first application-level Java process, forked from zygote

System Server is the first process started by the Zygote, but it’s important to note that many native processes start before zygote during the boot sequence.

During zygote initialization, it creates and loads the first JVM (either Dalvik or ART) and preloads libraries and classes that will be used by applications in the future. Efficient and fast app launch is achieved thanks 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.

After zygote initialization is complete, it then listens for connections on its socket for requests to start new apps. Remember, this is specifically about Java processes – many native processes are started before zygote.

The Copy-on-Write Magic:

When a user clicks on an app icon, Android doesn’t create everything from scratch. As mentioned in the previous section, the new application process is forked and a new VM is created, and the application is then bound to the thread of this process.

The COW mechanism works like this:

  • With COW, Linux doesn’t actually copy anything. On the contrary, it just maps the pages of the new process over to those of the parent process and makes copies only when the new process writes to a page
  • Parent and child processes both read the same memory pages until one of them needs to change that page
  • If either process wants to modify a memory page, only that specific page gets copied and changed for the requesting process

Thus, saving significant amount of memory and setup time as well. To add, in case of Android, these pages are never written, as the libraries are mostly immutable and hardly changed over the process lifetime.

This mechanism comes from Linux OS (which Android is based on) and enables Android to deliver high-speed process creation. This shows itself in fast app opening times and improved memory usage, allowing Android to run smoothly even on devices with limited RAM and CPU.

The Technical Benefits

Because each container receives a map, these resources are shared between applications, eliminating the need for each new fork of the VM to keep its own copy of classes and resources. This architecture provides several advantages:

  1. Faster app startup: Apps don’t need to load core libraries from scratch
  2. Better memory efficiency: Shared resources mean less memory usage per app
  3. Reduced garbage collection: Less work for the Java Runtime’s garbage collector
  4. Lower energy consumption: Less CPU work means better battery life

Conclusion:

The Zygote process creation model allows Android to run smoothly even when devices have limited RAM and CPU resources. This is the process which initializes a very first instance of Dalvik virtual machine. It also pre-loads all common classes used by Android application framework and various apps installed on a system.

This implementation comes from Unix server architecture, where daemons (background processes) listen on sockets for incoming requests. Similarly, Zygote listens for connections on its socket for requests to start new apps. When a request comes in, Zygote forks itself and starts the new app process, making Android app launches remarkably fast.

Understanding this architecture helps Android developers write more efficient applications by being aware of how the underlying system works and optimizing accordingly.

References:

Zugote Connection link

Zygote system server fork link

Leave a Reply

Your email address will not be published. Required fields are marked *