Dalvik virtual machine and the Java Virtual Machine has many similar features, support GC, JIT, JNI, and so on. The main difference is the file format and instruction set of the following two characteristics compared with the discussions.
Difference1: File Formats
Dalvik virtual machine using dex class file format, and JVM class file format is class. The difference is, we are familiar with the class bytecode file contains only one class, but dex file can contain multiple classes. The advantage is that multiple classes of constant and repeated strings will keep a certain extent, saves space, but also adapted to run on the phone.
Difference2: Instruction Set
Dalvik is register-based instruction set, and the JVM stack-based instruction set. The difference is that, based on the instruction register in the register addressing, but faster instruction length is long; stack-based instruction set shorter, but more CPU time. Which is similar to CISC and RISC contrast contrast. From transplant, is clearly a stack-based instruction set more portable, it does not register for the target machine to do any optimization. And before hosting machine instruction set can then run the program on the target machine register the program optimization, this optimization is the process of optimizing compiler, JIT is not similar to the dynamic optimization during operation, but the ultimate goal is the program was converted to machine language.
Here, for the Dalvik virtual machine memory management, JIT, garbage collection, JIT, JNI, and process and thread management are discussed.
Dalvik virtual machine's memory can be divided into three parts: Java Object Heap, Bitmap Memory and Native Heap.
Java Object Heap used to store Java objects, the size of its different mobile phone manufacturers have different settings, you can get the maximum value by ActivityManager of getMemoryClass, the value is the maximum amount of memory that can be used in Android applications.
Bitmap Memory for storing and processing picture. Before Android3.0, Bitmap Memory is part of the Native Heap, the size of included Java Object Heap, namely bitmap objects and other java memory used can not exceed the maximum JOH above. After Android3.0, Bitmap Memory allocated directly in JOH, easy to GC.
Native Heap local machine language for the allocation of memory.
In Android2.3 previous releases, garbage collection policy Dalvik virtual machine as follows:
Garbage collection thread during execution, other threads are stopped so that a portion of the garbage collection completely, so the result is caused by a garbage collection program due to suspension time is usually greater than 100ms.
After Android2.3, garbage collection thread and other threads are executed concurrently, may only be a part of the garbage collection, so garbage collection program due to suspension time is greatly reduced, usually less than 5ms.
JIT is carried out in the course of running the compiled code is called repeatedly converted into native machine code saved, this process is performed dynamically at run time, with respect to the terms of the optimization program is compiled, JIT will take time to run the program . After Android2.2 began to support JNI.
Java program to run on a virtual machine, the program needs to be translated into the target machine instructions to perform some functions need to call the target machine operating system interface to complete. Java local calls can help us in the Java code and C \ C ++ code, call the function with each other. Android NDK provides to help us achieve JNI.
Android process has two main features:
Android APP Each process has a Dalvik virtual machine instances, which guarantees multiple app at run time of each other.
Every Android application process is made of a Zygote process fork out. The Zygote process is created by the initialization process out. Zygote process at boot time, it creates a virtual machine instance, and in this instance all of the virtual machine Java core libraries are loaded up. Zygote process whenever you need to create an Android application process when it is achieved by copying itself, which is achieved by the fork system call. These are fork out Android application process, on the one hand is to copy the Zygote process virtual machine instances, on the other hand shared the same set of Java core libraries and Zygote process. Not only does the process of creating Android application process quickly, and because all of the Android application processes share the same core set of Java libraries and saves memory space.