Hot Article Guide| Click the title to read
How can programmers break through and improve themselves in the winter of the Internet?
If you suddenly get laid off, what is your Plan B?
The difference between employees who leave within 3 months and those who leave after about 2 years is beyond your imagination!
Author: Cat Eating Fish
Link: https://juejin.im/post/5d4bdb23e51d453c2577b747
After reading this article, you will:
-
Understand how computers interpret the programs we write and execute corresponding functions -
Learn about the evolution of the Android virtual machine -
Understand the three main reasons for Android lagging from the bottom up
First, we need to review some basic concepts to understand how computers interpret the programs we write and execute corresponding functions.
1. Compilation & Interpretation
Some programming languages (like Java) can be understood by computers through a compilation-interpretation process
Let’s take a piece of Java code
public static void main(String[] args){
print('Hello World')
}
This is the first lesson for all programmers. Just write this code and execute it, and the computer or phone will print out Hello World.
So the question arises, English is the language of the human world, how does the computer (CPU) understand English?
2. Machine Code & Bytecode
Machine Code
Machine code is the language that can be directly interpreted and executed by the CPU.
Bytecode
Chinese person A cannot understand the Russian exam paper, Russian person B cannot understand the Chinese exam paper, but everyone can understand the English exam paper.
Bytecode is an intermediate code; Java can compile to bytecode, and the same bytecode can be interpreted into the specified machine code according to the specified template rules.
Benefits of Bytecode:
1. Achieves cross-platform compatibility; one source code only needs to be compiled into one bytecode, and then the bytecode can be interpreted into the machine code recognized by the current computer according to different templates, which is what Java refers to as “compile once, run anywhere.”
2. The size of the bytecode compiled from the same source code is much smaller than the machine code.
3. Compiled Languages & Interpreted Languages
Compiled Languages
The well-known C/C++ languages are compiled languages, meaning that after compilation, programmers can directly compile them into machine code, which can be directly interpreted and executed by the CPU.
This is also one of the reasons why Apple phones are smoother! (No middlemen making profits)
Compiled-Interpreted Languages
Taking the language Java used for Android development as an example, Java is a compiled-interpreted language, meaning that after compilation, programmers cannot directly compile it into machine code but will compile it into bytecode (in Java programs as .class files, in Android programs as .dex files). Then we need to interpret the bytecode into machine code to make it understandable by the CPU.
This second interpretation, that is, the process of interpreting from bytecode to machine code, is implemented in the Java virtual machine after the program is installed or run.
1. Virtual Machine – Slow Interpretation Process
From the above description, we can know that iOS does not lag because it goes directly to the hardware layer and skips the intermediate interpretation steps. In contrast, Android, due to the absence of a direct approach, needs to interpret into machine code in real-time each time it executes, resulting in significantly lower performance compared to iOS.
① Android 1.0 Dalvik (DVM) + Interpreter
② Android 2.2 DVM + JIT
-
Opening the app will slow down -
Every time the app is opened, it requires repeated labor, which cannot be done once and for all. -
If I suddenly order a dish that I have never ordered before, I will have to wait for it. If the user opens something that JIT has not prepared, they can only wait for the interpreter in DVM to execute and interpret.
③ Android 5.0 ART + AOT
④ Android 7.0 Hybrid Compilation
⑤ Android 8.0 Improved Interpreter
⑥ Android 9.0 Improved Compilation Template
2. JNI – Slow Invocation Between Java and C
JNI, also known as Java Native Interface, is used for interacting with C/C++ code.
3. Compilation Template for Bytecode – Not Optimized for Specific Apps
Let’s use an example to understand the compilation template; “Hello world” can be translated as “你好,世界”, and it can also be translated as “世界,你好”. The difference is due to the different compilation templates.
Leave a Comment
Your email address will not be published. Required fields are marked *