Take the 2-minute tour ×

I try to understand how Android lauch applications. The question is how does zygote exactly fork Dalvik VM. I dont really understand why it is not possible to run multiple applications in the same Dalvik VM.

share|improve this question

3 Answers 3

up vote 22 down vote accepted

No. Dalvik doesn't span processes.

However, the Binder IPC mechanism can do a very convincing job of making objects appear to migrate to a different process and its Dalvik instance. Also, the memory management is very good about sharing read-only pages across all processes that need them. The Dalvik process hosting a typical app is forked off of zygote with all the common android libraries already mapped, so new unique copies don't have to be opened.

Source: Do apps using multiple processes share a Dalvik instance?

Also check these links:



share|improve this answer
Thak you very much!!!! –  Aprel Feb 6 '12 at 9:46

Q. how does zygote exactly fork Dalvik VM?

Short Answer: Zygote process basically cold boots a VM on system start up. Once done, it listens to a socket for incoming commands. Other processes (e.g. ActivityManagerService) writes commands to this socket, whenever it needs a new process for an application. This command is read by the Zygote process and calls fork() - so the child process now gets a pre-warmed up VM in which to run. This is how zygote forks the Dalvik VM.

Long answer: (this could be really long) After kernel is loaded, init.rc is parsed and native services are started. With this, the /system/bin/app_process (Source code: frameworks/base/cmds/app_process/app_main.cpp) is run. This eventually calls AndroidRuntime.start() function (Source code: frameworks/base/core/jni/AndroidRuntime.cpp), passing it the parameters com.android.internal.os.ZygoteInit and start-system-server.

The AndroidRuntime.start() starts the Java VM, then calls ZygoteInit.main() (Source code: frameworks/base/core/java/com/android/internal/os/ZygoteInit.java), passing it the parameter start-system-server.

ZygoteInit.main() first registers the zygote socket (the zygote process listens to a socket for incoming commands, and on receiving new command, spawns a new process as requested). Next thing that happens is, it preloads a lot of classes (which is listed in frameworks/base/preloaded-classes, last I checked, 2307 lines, in android 4.0.4) and all the system-wide resources like drawables, xmls, etc. Then it calls startSystemServer() which forks a new process for com.android.server.SystemServer (Source code: frameworks/base/services/java/com/android/server/SystemServer.java). Forking of system server is a special case, because in all other cases, zygote process listens to a socket and forks for new commands, as we will see soon.

After SystemServer is forked, a function runSelectLoopMode() is called. This is a while(true) loop which basically establishes a ZygoteConnection with the zygote socket and waits for commands on it. When a command is received, ZygoteConnection.runOnce() is called (Source code: frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java)

ZygoteConnection.runOnce() calls Zygote.forkAndSpecialize() (Source code: libcore/dalvik/src/main/java/dalvik/system/Zygote.java) which simply calls a natvie function to do the fork. Thus, like in the case of SystemServer, a child process is created which has a pre-warmed up Dalvik for itself.

Q. why it is not possible to run multiple applications in the same Dalvik VM?

This is a design decision as far as I know. Android guys just decided to fork a new VM per process, for security via sandboxing.

share|improve this answer

Zygote is also used to share the system drawables with all the apps. This allows the system to load the bitmaps for buttons only once for instance.

share|improve this answer

Your Answer


