The document describes the Java Virtual Machine (JVM), Dalvik virtual machine, and Android Runtime (ART). It explains that the JVM interprets Java bytecode, Dalvik was developed for Android and compiles .class files to .dex files, and ART replaced Dalvik by compiling Dalvik bytecode to native instructions for improved performance. It then provides an example of Java bytecode for a sample method and step-by-step interpretation of the bytecode instructions.
2. What are JVM and Dalvik?
Java Virtual machines:
An abstract computing machines that enable
running of a Java program.
3. What is Art?
Application runtime environment that translates
application bytecode into native instructions, that
are later executed by it.
4. Java Virtual Machine
● Interprets Java bytecode instructions
● Stack based
● Stack based easy to interpret on many architectures
● Classloader loads, verifies, resolves and links .class files which contain
instructions on runtime
● Byte long opcodes, out of 255 possible, 198 are in use
● Many languages can be compiled to Java bytecode
5. Dalvik
● Class library based on Apache Harmony JVM, not Oracle JVM
● Developed by Dan Borenstein along with Android, named after town in
Iceland
● Register based: optimized for ARM platformance
● Low memory footprint, optimized for running several instances at the same
time
● .class files are compiled into .dx, resolved and merged into .dex files compile
time
● Longer instructions, less of them
● JIT added in Froyo (2.2) to improve performance
● JIT saves ODEX files in /data/dalvik-cache
6.
7. Art
● Previewed in KitKat, replaced Dalvik in Lollipop
● No more JIT, uses AOT compilation
● Lower memory footprint, more disk used
● Same input bytecodes (DEX) as Dalvik, compiled into native (elf) executables
● Improved GC, power consumption and performance
● Art files: memory mapping is currently fixed (defeating ASLR)
● Not really 64 bit, VM architecture is 32 bit, getting there
● Art format constantly changing, undocumented and proprietary
● Supports ARM, ARM_64, x86 and Mips
8. Bytecode example: prep
private static int calculate() {
int ret = -321;
for(int i = 0; i < 10000 ; i++) {
ret += i%5;
}
return ret;
}
> javac -target 1.7 -source 1.7 Test.java
> dx --dex --output="Test.dex" "Test.class"
> r2 Test.class
> r2 Test.dex