Skip to main content

Command Palette

Search for a command to run...

Zygote

Published
•3 min read•View as Markdown

Good. This is a core Android internals question — and for 6+ years experience, your answer must go deeper than “it starts apps”.

Let’s break it properly.


🧬 What is Zygote?

Zygote is the parent process of all Android application processes.

It is started during Android boot and is responsible for:

  • Preloading core Java classes

  • Preloading system resources

  • Starting the ART runtime

  • Forking new app processes


🔄 Where Does Zygote Sit in Android?

Simplified boot flow:

Bootloader
   ↓
Linux Kernel
   ↓
init
   ↓
Zygote
   ↓
System Server
   ↓
App Processes

🚀 What Exactly Does Zygote Do?

1️⃣ Starts the ART Runtime

Zygote initializes:

  • ART (Android Runtime)

  • Heap

  • Class loader

  • Core framework classes


2️⃣ Preloads Classes and Resources

It loads:

  • android.* framework classes

  • Resources

  • Drawables

  • System libraries

Why?

To use Copy-On-Write (COW) memory optimization.


3️⃣ Forks New App Processes

When you tap an app:

  1. Launcher → ActivityManagerService (AMS)

  2. AMS checks if process exists

  3. If not → AMS asks Zygote to fork

  4. Zygote forks a new Linux process

The new process inherits:

  • Preloaded classes

  • Memory pages

  • Runtime setup

Then:

  • ActivityThread.main() runs

  • Application starts


🧠 Why Not Create New Process From Scratch?

Because:

  • Starting VM from scratch is slow

  • Loading framework classes is heavy

  • Memory duplication is expensive

Zygote uses:

🔥 Copy-On-Write (COW)

Memory pages are shared between processes until modified.

So:

  • Faster startup

  • Lower memory usage

  • Better performance


🔍 What Is Fork?

Zygote uses Linux fork() system call.

After fork:

  • Parent → Zygote

  • Child → New app process

Child gets:

  • Same memory

  • Same file descriptors

  • Same VM state

Then child replaces some parts and runs app code.


🏗 Zygote Also Starts System Server

During boot:

Zygote forks:

system_server

System server hosts:

  • ActivityManagerService

  • PackageManagerService

  • WindowManagerService

  • etc.


🔥 Advanced (Interview Level)

There are actually:

  • 32-bit Zygote

  • 64-bit Zygote

On modern devices:

  • zygote

  • zygote64


🔎 What Happens Internally When App Launches?

  1. AMS sends command to Zygote via socket

  2. Zygote receives command

  3. Calls fork()

  4. In child:

    • Set UID/GID

    • Set SELinux context

    • Call RuntimeInit

    • Start ActivityThread


🧠 Very Important Interview Question

❓ Why does Android use fork instead of exec?

Answer:

  • exec replaces memory

  • fork allows Copy-On-Write sharing

  • Faster startup

  • Lower memory footprint


⚠️ What If Zygote Crashes?

  • All apps die

  • System_server dies

  • Device reboots

Zygote is critical system component.


🎯 One-Line Answer

Zygote is a pre-initialized Android runtime process that forks new application processes using Linux fork() to enable fast startup and memory sharing through Copy-On-Write.