Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off ALL optimization by Dalvik VM

So I'm trying to write some low-level code for Android, and my main concern is that I want to avoid ALL optimization by the JIT compiler (or anything else). After doing some research, the best approach seems to be to:

  1. write Java bytecode by hand
  2. convert it to a dex file using the "dx" command
  3. run it on the program using the "dalvikvm" command (via adb shell) with the "-Xverify:none -Xdexopt:none" paramaters specified

My question is: will this in fact avoid ALL optimization? The previous discussion here https://groups.google.com/forum/#!topic/android-platform/Y-pzP9z6xLw makes me unsure, and I can't 100% convince myself by reading the docs.

Any confirmation one way or the other is greatly appreciated.

like image 264
lietuviuHimnas Avatar asked Dec 18 '13 16:12

lietuviuHimnas


People also ask

What is the Dalvik virtual machine in Android?

The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices. It optimizes the virtual machine for memory, battery life and performance. Dalvik is a name of a town in Iceland. The Dalvik VM was written by Dan Bornstein.

Does Android still use Dalvik?

Dalvik is a discontinued process virtual machine (VM) in the Android OS that executes applications written for Android. Dalvik bytecode format is still used as a distribution format, but no longer at runtime in newer Android versions.

What is the difference between ART runtime and Dalvik runtime?

Difference between ART and Dalvik Approach: ART uses AOT(Ahead Of Time) approach and compiles the whole code during the installation time but the Dalvik uses JIT(Just In Time) approach and complies only a part of the code during installation and rest of the code will be compiled dynamically.

What is Dalvik and ART?

ART and its predecessor Dalvik were originally created specifically for the Android project. ART as the runtime executes the Dalvik Executable format and Dex bytecode specification. ART and Dalvik are compatible runtimes running Dex bytecode, so apps developed for Dalvik should work when running with ART.


1 Answers

Some of the instruction rewriting performed by dexopt cannot be disabled. For example, accesses to volatile long fields must be handled differently from access to long fields, and the specialization is handled by replacing the field-get instruction with a different instruction.

The optimizations performed by dexopt take the form of instruction replacement, usually some sort of "quickening" that allows the VM to do a little less work. All such optimizations are performed statically, ahead of time, not dynamically at run time, so you will get consistent behavior. Enabling the dexopt optimizations doesn't introduce unknowns, it just changes from one set of knowns to a different set of knowns.

The biggest source of variation is going to be Dalvik's JIT compiler, which you can disable with -Xint:fast. See this slightly outdated doc for notes on how to configure this system-wide.

like image 196
fadden Avatar answered Sep 20 '22 21:09

fadden