Android Development The difference between a Java virtual machine and a Dalvik virtual machine

Source: Internet
Author: User
Tags garbage collection java se

The Dalvik virtual machine relies on the underlying POSIX-compliant operating system, which can simply complete process isolation and thread management. It has efficient use of memory and high performance on Low-speed CPUs. Each Android application has a separate Dalvik virtual machine instance at the bottom, and its code is executed with the virtual machine interpretation. Dalvik virtual machines are not implemented according to the specifications of the Java Virtual machine, and are not compatible with the JVM; there are two obvious differences: the Java Virtual machine runs Java bytecode, while the Dalvik virtual machine runs its proprietary file format, Dex (Dalvik Executable). Java classes in the Java SE program are compiled into one or more bytecode files (. Class) and then packaged into jar files, and then Java virtual opportunities get the corresponding byte code from the corresponding class file and Jar file Android apps are programmed in the Java language, but after being compiled into a class file, a tool (DX) is used to convert all of the class files into a Dex file and then dalvik the virtual opportunity to read the instructions and data from it.

Dalvik and Android are the next generation of Linux based open source handset operating systems, and the system architecture can be divided into the following parts: Linux kernel local library Android Runtime application Framework Application

As shown in the image above, part of the Android architecture is called the Android Runtime, the Android runtime environment, which includes two parts, one is the core class library of Android, and the other is the Dalvik virtual machine.

As shown in the figure above, the JVM compiles. Java text into a. Class bytecode file, and when executing Java programs, the class loader loads all the required classes into memory.

The diagram above shows the structure of loading bytecode files into memory, and each class file is parsed into several parts, including headers (mainly JDK versions, etc.), constants, and so on. How many classes there are in memory, the number of files in the diagram structure is generated.


The above illustration shows a class file structure diagram loaded by the Dalvik virtual machine. The Davik virtual machine compiles the. java file into a. class file and converts the. class file into a. dex file, Dalvik to execute the. dex file.

In fact, the. dex file is to put the constants, methods, and so on in multiple class files together. form the structure as shown in the above figure. There is a constant pool in each class file, managed by a constant pool in the Dex file.

The JVM is a stack based architecture on the schema, so every access to the data CPU is in memory to fetch the data.

The Dalvik is a register based architecture. Register is a piece of storage space on the CPU, if the CPU directly from the register to read the data will be much faster.

the difference between a Java virtual machine and a Dalvik virtual machine:


The td>

java Virtual machine Dalvik virtual machine

Java virtual machines are based on stack .   A stack-based machine must use instructions to load and manipulate data on the stack, instructions more, more Dalvik virtual machines are based on registers

Java Virtual machine is running Java byte code. (Java classes are compiled into one or more bytecode. class files, packaged into a. jar file, and the Java Virtual machine obtains the appropriate bytecode from the corresponding. class file and the. jar file)

 

Dalvik runs a custom. Dex byte code format. (When a Java class is compiled into a. class file, All. class files are converted to a. dex file through a DX tool, and then dalvik the virtual opportunity to read the instructions and data from it)

constant pool has been modified to use only 32-bit indexes to simplify the interpreter. Dalvik's heap and stack parameters can be changed by-XMS and-xmx


An application, a virtual machine instance, a process (all Android The applied threads are all corresponding to a Linux thread, all running in their own sandbox, and different applications running in different processes. Each Android Dalvik application is given a standalone Linux PID (app_*))


One of the primary differences between Dalvik and standard Java virtual machines (JVMS) is that the Dalvik is based on registers and the JVM is based on stacks. Another major difference between Dalvik and Java is that the operating environment is--dalvik optimized to allow instances of multiple virtual machines to run concurrently in limited memory, and each Dalvik application executes as a stand-alone Linux process. (1) The virtual machine is very small, the space used is small, (2) Dalvik has no JIT compiler, (3) The constant pool has been modified to use only 32-bit index to simplify the interpreter, (4) it uses its own bytecode, not Java bytecode.
(5) Dalvik is suitable for machines with smaller memory capacity and less data processing capability, so it is suitable for mobile terminals. Since most of the Delvik virtual machine directives contain the address of a register, its instructions are usually longer than the Java Virtual machine's instructions. In general, a stack based machine requires more instructions, and machine instructions based on registers are longer.

What's the difference based on stacks and registers?

The concept of stacks

Stacks are unique to threads, saving their running state and local automatic variables (so that local variables in multiple threads are independent of each other, unlike class variables). The stack is initialized at the beginning of the thread (the start method of the threads, initializing the allocation stack), and each thread's stack is independent of each other. Each function has its own stack, which is used to pass arguments between functions. The operating system will automatically switch the stack when switching threads, which is to switch SS/ESP registers. Stack space does not need to be explicitly allocated and released within a high-level language. The concept of registers:

Registers are part of the CPU. Registers are a high speed storage unit of limited storage capacity, which can be used for staging instructions, data, and addresses. In the control parts of the central processing Unit, the registers included are instruction registers (IR) and program counters (PCS), and in the arithmetic and logic parts of the central processing Unit, the registers included are accumulators (ACC).

Based on the implementation of the stack virtual machine, the implementation of virtual machine based on register is less than the hardware versatility, but it is more efficient in the execution of the code, the virtual machine instruction in the interpretation of the time spent mainly in the following three: distribution instructions, access operations, execution operations, which distribution instructions this link to the performance of the greatest impact. In a register based virtual machine, it is more effective to reduce the distribution of redundant instructions and reduce the memory read-write access.

Dalvik Virtual machine schema:

In the Android source code, the implementation of the Dalvik virtual machine is located in the "dalvik/" directory, where "DALVIK/VM" is the implementation of the virtual machine part, will be compiled into libdvm.so, and "Dalvik/libdex" will be compiled into a libdex.a static library as the Dex tool; "Dalvik/dexdump" is the decompile tool for the. dex file, and the executable program for the virtual machine is located in "DALVIK/DALVIKVM" and will be compiled into DALVIKVM executables.

Dalvik Virtual Machine Schema:


android application compiling and running process:


Dalvik Process Management:

Dalvik process Management relies on the Linux process architecture to create a process for the application that uses the Linux fork mechanism to replicate a process (the replication process is often more efficient than the creation process).

Zygote is a virtual machine process and an incubator for virtual machine instances, which is launched through the INIT process. The first hatch is the System_server (Android most system service daemon, which listens to the socket waiting for the request command, and when an application starts, it makes a request, Zygote will fork a new application process). Whenever a system requires an Android application to execute, Zygote uses Linux's fork to generate a subprocess to execute the application.

JVM and Dalvik process management:

There are many ways to communicate between processes in Linux, but Dalvik uses a signaling method to complete interprocess communication.

Android Initialization Process :



the difference between Dalvik and Java runtime environments:

1, Dalvik is mainly to complete the object lifecycle management, stack management, thread management, security and exception management, as well as garbage collection and other important functions.

2, Dalvik is responsible for process isolation and thread management, each Android application at the bottom will be a separate Dalvik virtual machine instance, the code in the interpretation of the virtual machine can be implemented.

3, unlike Java Virtual machine run Java bytecode, Dalvik virtual machine is running its proprietary file format Dex.

4, DEX file format can reduce the overall file size, improve the I/O operation of class lookup speed.

5, Odex is in order to further improve performance in the running process, the further optimization of Dex files.

6. Each Android application runs in a Dalvik virtual machine instance, and each virtual machine instance is a separate process space. The threading mechanism of virtual machines, memory allocation and management, mutexes, and so on are all dependent on the underlying operating system. All Android application threads correspond to a Linux thread, and virtual machines can thus rely more on the operating system's threading and management mechanisms

7, there is a special virtual machine process zygote, he is the virtual machine instance incubator. Whenever the system requires an Android application to execute, zygote fork a child process to execute the application. It will be generated when the system is started, it will complete the initialization of the virtual machine, library load, preset class library and initialization operations. If the system needs a new virtual machine instance, it will quickly replicate itself to provide the system at the fastest speed. For some read-only system libraries, all virtual machine instances share an area of memory with Zygote.

the process of packaging into APK:
The programming language used for Android applications is the Java language, like the Java SE, Compile-time uses SUNJDK to program Java source programs into standard Java bytecode files (. class files), and then convert all bytecode files into a Dex file (Classes.dex) via the tool software DX. Finally, use the Android Packaging tool (AAPT) to combine Dex files, resource files, and Androidmanifest.xml files (binary formats) into an application package (APK).


Related Article

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.