Android source code analysis -Dalvik virtual machine creation process

More complete project download. To be continued. Source. Graphic knowledge subsequent upload github.
You can click on my  link I get

I. Introduction Dalvik

 1.java operation needs JVM, the same used in android java language, requires a VM. For lack of mobile phone processors and memory and other hardware resources and the launch of a VM, provide an environment for running android called DVM.

 2.Dalvik virtual machine allows multiple instance of. In fact a per app in android is running in its own VM instance (Sandbox). Each VM instance in linux is a separate process, it can be considered the same concept. DVM run in their own process, a different app will not interfere with each other, and will not lead to the collapse of a DVM processes are all app crashes. In this regard, Android dvm process and Linux process, a process similar to the concept of the application.

3. The difference between the JVM:

  •  1. Based on different architectures. JVM is a stack-based architecture, DVM is register based architecture.
  • 2.jvm are running bytecode files, and run dvm dex file format you define.
     

    JVM compilation java-> class-> JAR
    DVM compilation java-> class-> dex

Dvm and summarize the difference between jvm:

A difference : dvm do is .dex format jvm execution is to produce .class .class file after file android program compiled, then, dex tool will .class file processed into .dex file, then the resource file and .dex files packaged into .apk file. apk android package is meant. jvm .class file is executed.
Difference between the two : dvm is register-based virtual machine while jvm execution is based virtualization stack virtual machines. Register access speed faster than the stack, DVM achieve maximum optimization of the hardware, more suitable for mobile devices.
Three differences : There is a lot of redundant information .class file, dex tool to remove redundant information, and to integrate all the .class files to .dex file. Reducing the I / O operation, to improve the search speed class

Photo understand dvm mainly to do:
Android source code analysis -Dalvik virtual machine creation process

Two .Dalvik boot process

//AndroidRuntime.cpp
int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv, bool zygote)
{
    JavaVMInitArgs initArgs;
    char propBuf[PROPERTY_VALUE_MAX];
    char stackTraceFileBuf[sizeof("-Xstacktracefile:")-1 + PROPERTY_VALUE_MAX];
    char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
    char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
    char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
    char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
    char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
    char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
    char usejitOptsBuf[sizeof("-Xusejit:")-1 + PROPERTY_VALUE_MAX];
    char jitmaxsizeOptsBuf[sizeof("-Xjitmaxsize:")-1 + PROPERTY_VALUE_MAX];
    char jitinitialsizeOptsBuf[sizeof("-Xjitinitialsize:")-1 + PROPERTY_VALUE_MAX];
    char jitthresholdOptsBuf[sizeof("-Xjitthreshold:")-1 + PROPERTY_VALUE_MAX];
    char useJitProfilesOptsBuf[sizeof("-Xjitsaveprofilinginfo:")-1 + PROPERTY_VALUE_MAX];
    char jitprithreadweightOptBuf[sizeof("-Xjitprithreadweight:")-1 + PROPERTY_VALUE_MAX];
    char jittransitionweightOptBuf[sizeof("-Xjittransitionweight:")-1 + PROPERTY_VALUE_MAX];
    char gctypeOptsBuf[sizeof("-Xgc:")-1 + PROPERTY_VALUE_MAX];
    char backgroundgcOptsBuf[sizeof("-XX:BackgroundGC=")-1 + PROPERTY_VALUE_MAX];
    char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
    char cachePruneBuf[sizeof("-Xzygote-max-boot-retry=")-1 + PROPERTY_VALUE_MAX];
    char dex2oatXmsImageFlagsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
    char dex2oatXmxImageFlagsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
    char dex2oatXmsFlagsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
    char dex2oatXmxFlagsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
    char dex2oatCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
    char dex2oatImageCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
    char dex2oatThreadsBuf[sizeof("-j")-1 + PROPERTY_VALUE_MAX];
    char dex2oatThreadsImageBuf[sizeof("-j")-1 + PROPERTY_VALUE_MAX];
    char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
    char dex2oat_isa_variant[sizeof("--instruction-set-variant=") -1 + PROPERTY_VALUE_MAX];
    char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
    char dex2oat_isa_features[sizeof("--instruction-set-features=") -1 + PROPERTY_VALUE_MAX];
    char dex2oatFlagsBuf[PROPERTY_VALUE_MAX];
    char dex2oatImageFlagsBuf[PROPERTY_VALUE_MAX];
    char extraOptsBuf[PROPERTY_VALUE_MAX];
    char voldDecryptBuf[PROPERTY_VALUE_MAX];
    ...

    /*
     * Initialize the VM.
     *
     * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
     * If this call succeeds, the VM is ready, and we can start issuing
     * JNI calls.
     */
    if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
        ALOGE("JNI_CreateJavaVM failed\n");
        return -1;
    }

    return 0;
}

void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
{

    /* start the virtual machine */
    JniInvocation jni_invocation;
    jni_invocation.Init(NULL);
    JNIEnv* env;
    if (startVm(&mJavaVM, &env, zygote) != 0) {
        return;
    }
    onVmCreated(env);

    /*
     * Register android functions.
     */
    if (startReg(env) < 0) {
        ALOGE("Unable to register all android natives\n");
        return;
    }

}

     //Dalvik虚拟机在Zygote进程中的启动过程,这个启动过程主要就是完成了以下四个事情:
        //1. 创建了一个Dalvik虚拟机实例;
        //2. 加载了Java核心类及其JNI方法;
        //3. 为主线程的设置了一个JNI环境;
        //4. 注册了Android核心类的JNI方法。

Zygote effect Dalvik start:

    1. Zygote process is ready for the Android system good a Dalvik virtual machine instances, after Zygote process when creating Android application process, you can copy its own Dalvik virtual machine instance to the newly created Android application process to go, which speeds up Android application startup process.
  • 2.Java core classes and Android core classes (located in dex files), as well as their method of JNI (located so file), memory mapping is a way to read, therefore, Zygote process in the creation of Android application process when, in addition to its own instance of the Dalvik virtual machine to copy the Android app newly created process, they can also share core Android Java core classes and classes, and their methods and Android JNI newly created application process, so it can save memory consumption.

  • To speed up the startup process 3.Zygote Android application process, they sacrificed their start-up speed, because it needs to load a lot of Java core classes, and register a large number of Android core classes JNI method. Dalvik virtual machine when loading Java core classes, but also they need to be verified and optimized, these are usually time-consuming. Also, because Zygote process is started by the init process, that process is Zygote boot time to boot, so the sacrifice Zygote process is relatively large. But after all, we are playing the phone, rarely shut down, that is rarely turned on, therefore, start sacrificing speed Zygote process is worth it, in exchange for quick launch of an Android application.

    More complete project download. To be continued. Source. Graphic knowledge subsequent upload github.
    You can click on my  link I get

Guess you like

Origin blog.51cto.com/14541311/2444231