Analysis of Dalvik VMS and JVM

Source: Internet
Author: User

Http://zh.wikipedia.org/wiki/Android

Http://zh.wikipedia.org/wiki/Dalvik virtual Machines

Http://zh.wikipedia.org/wiki/Java virtual Machines

I. Understanding of Dalvik VMS and JVMs

The JVM is a fictional runtime that runs Java programs by simulating the implementation of various computer functions on a real computer. It has a perfect hardware architecture (such as processor, stack, register, etc.), also has the corresponding instruction system, using the JVM is to make Java program support and operating system independent. Theoretically, in any operating system, a Java program can be run as long as there is a corresponding JVM.

The Dalvik VM is an Android program virtual machine running on an Android system whose instruction set is based on the Register schema (register-based) and performs a unique file format--dex bytecode to accomplish object lifecycle management, stack management, thread management, security Exception management , garbage collection and other important functions.

The development programming language for Android applications is Java, which runs on the JVM, but in practice, Dalvik does not comply with the JVM specification, and both are incompatible.

The compilation process is as follows:

JVM:. Java→.class→.jar

Dalvik VMS:. Java→.class→.dex (optimized) →.odex

From the Dalvik VM and JVM compilation process, the JVM runs the Java bytecode of the. class file, and the Dalvik VM runs its converted. dex file. The Dalvik VM reconstructs the. class, and the basic elements of the consolidation (constant pool, class definition, data segment) are finally compressed into a. dex file.

  

The JVM is a stack-based architecture, and bytecode is primarily 0 address forms. The Dalvik VM is based on the register schema, and its bytecode is primarily a mixed form of two address/three addresses. Two architectures who are faster? For VMS, the evaluation stack or register of the source schema may be simulated using the actual machine's memory, so the performance characteristics are different from the actual hardware. It is generally believed that the Dalvik VM based on the register architecture is more efficient than a stack-based JVM (generally speaking, the time for the interpretation of instructions in a VM is mainly spent on distributing instructions, accessing the operands, performing operations three aspects), although the 0 address instruction is more compact, completing the operation requires more load/ The store directive also means more command dispatch (instruction Dispatch) times and memory accesses; Access memory is an important bottleneck of execution speed, two address or three address although each instruction occupies more space, but overall can use less instruction to complete operation, There are fewer command allocations and memory accesses.

One difference in structure between. Dex bytecode and. Class bytecode is. Dex bytecode consolidates multiple files into one, so processing reduces overall file size, I/O operations, and also improves the class lookup speed, where the constant pool in each class file is now managed by a constant pool in the Dex file. The Dex file can also be further optimized:

1. Adjust the byte order (Little_endian) of all fields and align each domain in the structure

2. Verifying all classes in the Dex file

3, to some specific class awakened optimization, the method of the Operation code optimization

Optimized file size will increase, should be the original Dex file 1~4 times.

An Android application runs in a Dalvik virtual machine instance, and each virtual machine instance is a separate process space. Each process can pass through (Ipc,binder mechanism implementation). Virtual machine threading mechanisms, memory allocation management, mutexes, and so on are all implemented in reliance on the underlying operating system. Different applications run in different process spaces, and when a virtual machine shuts down or an unexpected abort does not affect other virtual machines, it can protect the security and operation of the application to the fullest extent.

Zygote is the incubator for virtual machine instances. The execution of Zygoteinit.main () in AndroidRuntime.cpp will complete a split, split child process that continues to initialize the Java layer's schema. Whenever a system requires an Android application to execute, zygote splits a child process to execute the program. The benefits of this are obvious: the zygote process is generated at system startup, it completes the initialization of the virtual machine, the loading of the library, the loading and initialization of the pre-set class library, and so on, while the system needs a new virtual machine instance, zygote through its own replication, the fastest to provide this system, in addition, For some read-only system libraries, all virtual machine instances share a chunk of memory with zygote, saving a significant amount of memory overhead.

Second, Dalvik VM and JVM Difference summary

1, Dalvik-based register (register Based), JVM-based stack (stack Based).

2, the core purpose of the JVM is to build a truly cross-platform, cross-instruction set of the program run environment, completely shielding the application and the connection between the OS. The purpose of DVM is to bring the local assets and environment of the Android OS to the application development with a unified interface.

3. The JVM uses. Class bytecode files, while Dalvik VMS use proprietary. DEX Bytecode files

4. Dalvik allows instances of multiple virtual machines to run concurrently in limited memory, and each Dalvik application executes as a standalone Linux process. A standalone process can prevent all programs from shutting down when the virtual machine crashes.

5. Dalvik virtual machines did not use instant-compile (JIT) technology in the early days. Starting with Android 2.2, the Dalvik virtual machine also supports JIT.

6, Dalvik VM through Zygote for class preload, Zygote will complete the initialization of the virtual machine, but also different from the JVM.

About Art:

Art mode is the abbreviation for Android runtime. Prior to Android 4.2, Android phone system applications were running on Dalvik Java virtual machines, which was also dependent on a compiler for communicating with the application. Every time the program is run, the code inside the program needs to be converted into machine code to run, which results in a relatively fast power consumption, high memory consumption, even if the flagship machine for a long time will also be a serious phenomenon.

Art mode and Dalvik mode, compared to the art mode is a good solution to this problem, by installing the application, the program is automatically compiled code pre-read, so that the program directly compiled into machine language, eliminating the Dalvik mode to convert code at all times, to achieve high efficiency, power saving, Consumes less system memory and runs smoothly on your phone. But there are always pros and cons, and art solves the problem at the same time: it takes up slightly higher storage space, and the installer has a little longer time to precompile than the normal Dalvik mode. However, Google said that the gap is not very large, and they will work to improve, so that its efficiency close to or more than Dalvik, the other drawback is that the native code occupies more storage space, but for the current phone and tablet storage space for this point of the more capacity requirements is not a problem.

Resources:

http://www.zhihu.com/question/20207106/answer/14654536

Http://book.51cto.com/art/201404/437195.htm

http://blog.csdn.net/x356982611/article/details/21983267

Dalvik VM and JVM analysis

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.