To learn the memory optimization of Android, first to understand the Java virtual machine, before I used a number of articles to introduce the knowledge of Java Virtual machine, is to pave the way for this series. In Android development we are in touch with Dalvik virtual machines and art virtual machines similar to Java virtual machines, and this is how we understand their fundamentals.
1.Dalvik Virtual Machine
Dalvik VM (Dalvik virtual machine), referred to as Dalvik VMs or DVM. It was written by Dan Bornstein, whose name originated in the small fishing village called Dalvik, which his ancestors had lived in. DVM is a virtual machine that Google has developed specifically for the Android platform, which runs in the Android runtime library. It is important to note that DVM is not a Java Virtual machine (hereinafter referred to as the JVM) and why, the following will give you an answer.
the difference between a DVM and a JVM
The reason that DVM is not a JVM is primarily due to the fact that DVM is not implemented in accordance with the JVM specification. There are two main differences between DVM and JVM.
based on a different schema JVM-based stack means that you need to go to the stack to read and write data, the required instructions will be more, this will cause slow, for the limited performance of mobile devices, obviously not very suitable. The DVM is a register-based, non-stack-based virtual machine that uses a large number of out-of-the-stack instructions to copy data, while the instructions are more compact and concise. However, because the display specifies the operand, the register-based instruction will be larger than the stack-based instruction, but the total code count will not increase as the number of instructions decreases.
does not execute a different bytecode in a Java SE program, The Java class is compiled into one or more. class files, packaged into jar files, and the JVM obtains the corresponding bytecode through the corresponding. class file and Jar file. The execution order is:. jar file,. java file. class file and DVM will convert all. class files to a. dex file with the DX tool, and the DVM will read the instructions and data from the. dex file. Execution order:. java file –>.class file-. dex file
as shown, the. jar file contains multiple. class files, each containing a constant pool of that class, class information, attributes, and so on. When the JVM loads the. jar file, it loads all the. class files inside it, which is slow to load and is not suitable for mobile devices with limited memory. In the. apk file, only one. dex file is included, and the. dex file contains all the information contained within the. class, so that loading increases speed. The. class file has a lot of redundancy information, and the Dex tool removes redundant information. and consolidate all the. class files into a. dex file, reducing I/O operations and increasing the lookup speed of classes.
DVM allows multiple processes to run concurrently in limited memory The DVM is optimized to allow multiple processes to run concurrently in limited memory. Each app in Android runs in a DVM instance, and each DVM instance runs in a separate process space. A standalone process can prevent all programs from shutting down when the virtual machine crashes.
DVM created and initialized by Zygote in the Android system boot process (ii) parsing the Zygote process initiation process I introduced Zygote, which can be called an incubator, which is a DVM process, and it is also used to create and initialize a DVM instance. Whenever the system needs to create an application, Zygote will Fock itself and quickly create and initialize a DVM instance for the application to run.
DVM Architecture
The DVM source code is located in the dalvik/directory, where the content of the DALVIK/VM directory is the specific implementation part of the DVM, it will be compiled into Libdvm.so;dalvik/libdex will be compiled into a libdex.a static library, as the Dex tool use; Dalvik /dexdump is the anti-compilation tool for. dex files; The DVM executable is located in DALVIK/DALVIKVM and will be compiled into DALVIKVM executables. The DVM schema is shown.
As you can see, the. class file compiled by the Java compiler is converted to a. dex file by the DX tool, the. dex file is processed by the ClassLoader, and the interpreter interprets and executes the Dalvik bytecode according to the instruction set, and finally the Linux processing.
the run-time heap of DVM
DVM consists of two spaces and a number of auxiliary data structures, and two spaces are Zygote space (Zygote heap) and allocation Space (Active Heap). Zygote space is used to manage the various objects preloaded and created by the Zygote process during startup, and the GC is not triggered in Zygote space, and all processes share the zone, such as system resources. Allocation space is created before the zygote process fork the first child process, which is a private process in which zygote processes and Fock child processes are allocated and disposed of on Allocation space. In addition to these two spaces, the following data structures are included:
- Card Table: For the DVM Concurrent GC, record spam after the first spam tag.
- Heap Bitmap: There are two heap Bitmap, one to record the last GC survived, and the other to record the object the GC survived.
- Mark STACK:DVM's run-time heap uses the tag-clear (Mark-sweep) algorithm to perform GC, without knowing the tag-clear algorithm of the classmate View Java Virtual machine (iv) garbage collection algorithm this article. Mark Stack is used in the GC's tagging phase, which is used to traverse the surviving objects.
2.ART Virtual Machine
Art (Android Runtime) is released by Android 4.4 to replace the Dalvik virtual, Android 4.4 by default or DVM, the system will provide an option to turn on art. At Android 5.0, the default is to use ART,DVM to exit the beef board from the historical stage.
the difference between art and DVM
Every time an application in a DVM runs, the bytecode needs to be converted to machine code via the instant compiler (Jit,just in), which makes the application less efficient to run. In art, the system will be pre-compiled (Aot,ahead of time) when the application is installed, pre-compiling bytecode into machine code and storing it locally, so that the application does not need to be compiled every time it is run, and the efficiency is greatly improved.
the run-time heap of art
Unlike the DVM GC, art has a variety of GC types, mainly divided into Mark-sweep GC and compacting GC. The space of the run-time heap of art is divided according to the different GC types, and if the Mark-sweep GC is used, the runtime heap consists of four space and several auxiliary data structures, four spaces are zygote space, Allocation Space, Image space, and large Object space. The role of Zygote space, Allocation space, and DVM is the same. Image space is used to store some pre-loaded classes, Large object space is used to allocate some large objects (the default size is 12k). where zygote space and image space are shared between processes. The run-time heap space with the Mark-sweep GC is divided as shown.
In addition to the four Space,art Java heaps, there are two mod Union Table, a card Table, two heap Bitmap, two object Map, and three object stacks. If you want to know more about them, refer to the Art runtime Java heap creation process analysis – Luo Shenyang this article.
Android memory Optimization (i) research on the principle of DVM and art