Zygote
Zygote is started from
/Frameworks/base/cmds/app_process/app_main.cpp
The main () function in.
Start JavaVM:
The main () function starts the VM:
If (zygote ){
Runtime. start ("com. android. internal. OS. ZygoteInit ",
StartSystemServer? "Start-system-server ":"");
While runtime is the object of AppRuntime, also in main:
AppRuntimeruntime;
AppRuntime is a subclass of AndroidRuntime. Its definition is in app_main.cpp:
ClassAppRuntime: public AndroidRuntime
Therefore, here, runtime. start () actually calls AndroidRuntime: start ().
The following is an analysis of AndroidRuntime: start:
/Frameworks/base/core/jni/AndroidRuntime. cpp
Start VM in the start () function:
/* Start the virtual machine */
JNIEnv * env;
If (startVm (& mJavaVM, & env )! = 0 ){
Return;
}
OnVmCreated (env );
The startVm () and onVmCreated () functions are also in AndroidRuntime. cpp.
The startVm () function uses the following code to call JNI to create a JavaVM:
/*
* Initialize the VM.
*
* The JavaVM * is essential per-process, and the JNIEnv * is per-thread.
* If this call succeeds, the VM is ready, and we can start issuing
* Jni cils.
*/
If (JNI_CreateJavaVM (pJavaVM, pEnv, & initArgs) <0 ){
LOGE ("JNI_CreateJavaVM failed \ n ");
Goto bail;
}
The onVmCreated () function is actually an empty function.
Void AndroidRuntime: onVmCreated (JNIEnv * env)
{
// If AndroidRuntime had anything to do here, we 'd have done it in 'start '.
}
At the same time, call the starregulatory () function in the start () function to register the JNI interface:
If (starregulatory (env) <0 ){
LOGE ("Unable to register all android natives \ n ");
Return;
}
Then in the start () function:
Env-> CallStaticVoidMethod (startClass, startMeth, strArray );
Call the main () function in com. android. internal. OS. ZygoteInit.
/Fameworks/base/core/java/com/android/internal/OS/ZygoteInit. java
Main () called:
RegisterZygoteSocket (); // registers the Listen port of the Socket to accept the request.
Preload ();
StartSystemServer (); // start SystemServer.
The preload () function is defined as follows:
Static void preload (){
PreloadClasses ();
PreloadResources ();
}
It mainly pre-loads classes and resources to speed up startup. The preload class list is saved in the/frameworks/base/preloaded-classes file.
After the preceding steps, Zygote is created.
Author: snsn1984