Android-based Android Virtual Machine

Source: Internet
Author: User

About Dalvik virtual machines
A very important feature of Java is its independence from the platform. Java virtual machines are used to achieve this feature.
. General advanced languages must at least compile different target codes to run on different platforms. While
After the Java Virtual Machine is introduced, the Java language does not need to be re-compiled when running on different platforms.
Java Virtual Machine shields information related to specific platforms, so that the Java language compiler only needs to be generated on the Java Virtual Machine
You can run the target code (bytecode) on multiple platforms without modification. The Java Virtual Machine is executing the word
The bytecode is interpreted as the machine command execution on a specific platform.
Dalvik was designed and used by Google to avoid copyright and authorization issues of Sun's Java me.

Java Virtual Machine on Android platform.

Main features of Dalvik virtual machines

Proprietary Dex File Format
Many classes are defined in an application. After compilation, many corresponding class files are generated.
The Dex file format integrates all the class files into one file. In this way,
Reduces the overall file size, I/O operations, and improves the speed of searching classes. The original constant pool in each class file,
A constant pool is used to manage Dex files.
One application, one virtual machine instance, and one process
Every android application runs in a Dalvik Virtual Machine instance, and every virtual machine instance is an independent
. The thread mechanism, memory allocation and management, and mutex of virtual machines depend on the underlying operating system.
. All Android Application threads correspond to one Linux thread, So virtual machines can depend more on the operating system.
Thread scheduling and management mechanism.
Different applications run in different process spaces, and different Linux users are used for applications from different sources to run
To maximize the Security and independent operation of applications.
Zygote is a virtual machine process and also an incubator for a virtual machine instance.
For Android applications, zygote will fork a sub-process to execute the application. The benefits of doing so are obvious.
Easy to see: the zygote process is generated when the system starts. It initializes the virtual machine, loads the library, and presets the class library.
But when the system needs a new Virtual Machine instance, zygote
Quickly provides a system. In addition, for some read-only system libraries, all Virtual Machine instances share the same disk with zygote.
Memory area, greatly saving memory overhead.
Register-based
Compared with stack-based Virtual Machine implementation, register-based Virtual Machine implementation is somewhat inferior in terms of hardware versatility
It is superior in code execution efficiency. In register-based virtual machines, redundancy can be reduced more effectively.
Distribute commands and reduce memory read/write access.

Dex Optimization

The Dex file structure is compact, but if we want to further improve the runtime performance, we will still

The Dex file needs to be further optimized. The optimization mainly targets the following aspects:
Adjust the byte order of all fields (little_endian) and each field in the alignment structure
Verify all classes in the DEX File
Optimize some specific classes and the operation codes in the methods
The optimized file size will increase, which should be 1-4 times that of the original Dex file.
There are two timing for optimization:
For preset applications, an optimization file can be generated after the system compilation, ending with odex. In this way
In addition to the APK file (excluding Dex), there is a corresponding odex file;
For non-preset applications, the DEX files contained in the APK file will be optimized at runtime, and the optimized files will
Stored in the cache.
Code call process:

Java programs and virtual machines

After the zygote process in the incubator of the VM instance is started, the zygote socket (/dev/socket/zygote) will be registered for detection.
Listen to and process requests that run the bytecode program.
Refer to init. RC line 243:
Service zygote/system/bin/app_process-xzygote/system/bin -- zygote
-- Start-system-Server
Socket zygote stream 666
Onrestart write/sys/android_power/request_state wake
Onrestart write/sys/power/State on
Init sets the parameter "-- zygote" to enter the service mode. The specific implementation is in the Java class
Com. Android. Internal. OS. zygoteinit
After the zygote process receives a request from the application (when you need to run <activity> in the manifest file,
<Service>, <receiver> and <provider>, the startup command is sent to zygote through socket.
Fork generates a sub-process, which is the virtual machine used to run the application. Virtual opportunity addition from zygote fork
Classes and methods of Java programs are loaded, and the Java program is interpreted and run through its own Java interpreter.
The process is created by submitting a request to the zygote server.
Refer to frameworks/base/CORE/Java/Android/OS/process. Java:
PID = zygotesendargsandgetpid (argsforzygote );
After zygote receives the command, fork a sub-process (zygoteconnection. Java) in the runonce () function ):
PID = zygote. forkandspecialize (parsedargs. uid, parsedargs. GID, parsedargs. gids,
Parsedargs. debugflags, rlimits );
The Dalvik virtual machine also uses the main method of the input class as the entry.
At the end of the runtimeinit. zygoteinit () function, invokestaticmain (startclass, startargs) is called
The command line parameter is loaded as the class name and its main function is executed. In this way, the user's Java program will eventually run.

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.