Android4.0.3 source code analysis-startup process Zygote

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.