• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3    <title>Controlling the Embedded VM</title>
4    <link rel=stylesheet href="android.css">
5</head>
6
7<body>
8<h1>Controlling the Embedded VM</h1>
9
10<ul>
11    <li><a href="#introduction">Introduction</a> (read this first!)
12    <li><a href="#checkjni">Extended JNI Checks</a>
13    <li><a href="#assertions">Assertions</a>
14    <li><a href="#verifier">Bytecode Verification and Optimization</a>
15    <li><a href="#execmode">Execution Mode</a>
16    <li><a href="#dp">Deadlock Prediction</a>
17    <li><a href="#stackdump">Stack Dumps</a>
18    <li><a href="#dexcheck">DEX File Checksums</a>
19    <li><a href="#general">General Flags</a>
20</ul>
21
22<h2><a name="introduction">Introduction (read this first!)</a></h2>
23
24<p>The Dalvik VM supports a variety of command-line arguments
25(use <code>adb shell dalvikvm -help</code> to get a summary), but
26it's not possible to pass arbitrary arguments through the
27Android application runtime.  It is, however, possible to affect the
28VM behavior through certain system properties.
29
30<p>For all of the features described below, you would set the system property
31with <code>setprop</code>,
32issuing a shell command on the device like this:
33<pre>adb shell setprop &lt;name&gt; &lt;value&gt;</pre>
34
35<p><strong>The Android runtime must be restarted before the changes will take
36effect</strong> (<code>adb shell stop; adb shell start</code>).  This is because the
37settings are processed in the "zygote" process, which starts early and stays
38around "forever".
39
40<p>You may not be able to set <code>dalvik.*</code> properties or restart
41the system as an unprivileged user.  You can use
42<code>adb root</code> or run the <code>su</code> command from the device
43shell on "userdebug" builds to become root first.  When in doubt,
44<pre>adb shell getprop &lt;name&gt;</pre>
45will tell you if the <code>setprop</code> took.
46
47<p>If you don't want the property to evaporate when the device reboots,
48add a line to <code>/data/local.prop</code> that looks like:
49<pre>&lt;name&gt; = &lt;value&gt;</pre>
50
51<p>Such changes will survive reboots, but will be lost if the data
52partition is wiped.  (Hint: create a <code>local.prop</code>
53on your workstation, then <code>adb push local.prop /data</code>.  Or,
54use one-liners like
55<code>adb shell "echo name = value &gt;&gt; /data/local.prop"</code> -- note
56the quotes are important.)
57
58
59<h2><a name="checkjni">Extended JNI Checks</a></h2>
60
61<p>JNI, the Java Native Interface, provides a way for code written in the
62Java programming language
63interact with native (C/C++) code.  The extended JNI checks will cause
64the system to run more slowly, but they can spot a variety of nasty bugs
65before they have a chance to cause problems.
66
67<p>There are two system properties that affect this feature, which is
68enabled with the <code>-Xcheck:jni</code> command-line argument.  The
69first is <code>ro.kernel.android.checkjni</code>.  This is set by the
70Android build system for development builds.  (It may also be set by
71the Android emulator unless the <code>-nojni</code> flag is provided on the
72emulator command line.)  Because this is an "ro." property, the value cannot
73be changed once the device has started.
74
75<p>To allow toggling of the CheckJNI flag, a second
76property, <code>dalvik.vm.checkjni</code>, is also checked.  The value
77of this overrides the value from <code>ro.kernel.android.checkjni</code>.
78
79<p>If neither property is defined, or <code>dalvik.vm.checkjni</code>
80is set to <code>false</code>, the <code>-Xcheck:jni</code> flag is
81not passed in, and JNI checks will be disabled.
82
83<p>To enable JNI checking:
84<pre>adb shell setprop dalvik.vm.checkjni true</pre>
85
86<p>You can also pass JNI-checking options into the VM through a system
87property.  The value set for <code>dalvik.vm.jniopts</code> will
88be passed in as the <code>-Xjniopts</code> argument.  For example:
89<pre>adb shell setprop dalvik.vm.jniopts forcecopy</pre>
90
91<p>For more information about JNI checks, see
92<a href="jni-tips.html">JNI Tips</a>.
93
94
95<h2><a name="assertions">Assertions</a></h2>
96
97<p>Dalvik VM supports the Java programming language "assert" statement.
98By default they are off, but the <code>dalvik.vm.enableassertions</code>
99property provides a way to set the value for a <code>-ea</code> argument.
100
101<p>The argument behaves the same as it does in other desktop VMs.  You
102can provide a class name, a package name (followed by "..."), or the
103special value "all".
104
105<p>For example, this:
106<pre>adb shell setprop dalvik.vm.enableassertions all</pre>
107enables assertions in all non-system classes.
108
109<p>The system property is much more limited than the full command line.
110It is not possible to specify more than one <code>-ea</code> entry, and there
111is no way to specify a <code>-da</code> entry.  There is presently no
112equivalent for <code>-esa</code>/<code>-dsa</code>.
113
114
115<h2><a name="verifier">Bytecode Verification and Optimization</a></h2>
116
117<p>The system tries to pre-verify all classes in a DEX file to reduce
118class load overhead, and performs a series of optimizations to improve
119runtime performance.  Both of these are done by the <code>dexopt</code>
120command, either in the build system or by the installer.  On a development
121device, <code>dexopt</code> may be run the first time a DEX file is used
122and whenever it or one of its dependencies is updated ("just-in-time"
123optimization and verification).
124
125<p>There are two command-line flags that control the just-in-time
126verification and optimization,
127<code>-Xverify</code> and <code>-Xdexopt</code>.  The Android framework
128configures these based on the <code>dalvik.vm.dexopt-flags</code>
129property.
130
131<p>If you set:
132<pre>adb shell setprop dalvik.vm.dexopt-flags v=a,o=v</pre>
133then the framework will pass <code>-Xverify:all -Xdexopt:verified</code>
134to the VM.  This enables verification, and only optimizes classes that
135successfully verified.  This is the safest setting, and is the default.
136<p>You could also set <code>dalvik.vm.dexopt-flags</code> to <code>v=n</code>
137to have the framework pass <code>-Xverify:none -Xdexopt:verified</code>
138to disable verification.  (We could pass in <code>-Xdexopt:all</code> to
139allow optimization, but that wouldn't necessarily optimize more of the
140code, since classes that fail verification may well be skipped by the
141optimizer for the same reasons.)  Classes will not be verified by
142<code>dexopt</code>, and unverified code will be loaded and executed.
143
144<p>Enabling verification will make the <code>dexopt</code> command
145take significantly longer, because the verification process is fairly slow.
146Once the verified and optimized DEX files have been prepared, verification
147incurs no additional overhead except when loading classes that failed
148to pre-verify.
149
150<p>If your DEX files are processed with verification disabled, and you
151later turn the verifier on, application loading will be noticeably
152slower (perhaps 40% or more) as classes are verified on first use.
153
154<p>For best results you should force a re-dexopt of all DEX files when
155this property changes.  You can do this with:
156<pre>adb shell "rm /data/dalvik-cache/*"</pre>
157This removes the cached versions of the DEX files.  Remember to
158stop and restart the runtime (<code>adb shell stop; adb shell start</code>).
159
160<p>(Previous version of the runtime supported the boolean
161<code>dalvik.vm.verify-bytecode</code> property, but that has been
162superceded by <code>dalvik.vm.dexopt-flags</code>.)</p>
163
164
165<h2><a name="execmode">Execution Mode</a></h2>
166
167<p>The current implementation of the Dalvik VM includes three distinct
168interpreter cores.  These are referred to as "fast", "portable", and
169"debug".  The "fast" interpreter is optimized for the current
170platform, and might consist of hand-optimized assembly routines.  In
171constrast, the "portable" interpreter is written in C and expected to
172run on a broad range of platforms.  The "debug" interpreter is a variant
173of "portable" that includes support for profiling and single-stepping.
174
175<p>The VM may also support just-in-time compilation.  While not strictly
176a different interpreter, the JIT compiler may be enabled or disabled
177with the same flag.  (Check the output of <code>dalvikvm -help</code> to
178see if JIT compilation is enabled in your VM.)
179
180<p>The VM allows you to choose between "fast", "portable", and "jit" with an
181extended form of the <code>-Xint</code> argument.  The value of this
182argument can be set through the <code>dalvik.vm.execution-mode</code>
183system property.
184
185<p>To select the "portable" interpreter, you would use:
186<pre>adb shell setprop dalvik.vm.execution-mode int:portable</pre>
187If the property is not specified, the most appropriate interpreter
188will be selected automatically.  At some point this mechanism may allow
189selection of other modes, such as JIT compilation.
190
191<p>Not all platforms have an optimized implementation.  In such cases,
192the "fast" interpreter is generated as a series of C stubs, and the
193result will be slower than the
194"portable" version.  (When we have optimized versions for all popular
195architectures the naming convention will be more accurate.)
196
197<p>If profiling is enabled or a debugger is attached, the VM
198switches to the "debug" interpreter.  When profiling ends or the debugger
199disconnects, the original interpreter is resumed.  (The "debug" interpreter
200is substantially slower, something to keep in mind when evaluating
201profiling data.)
202
203<p>The JIT compiler can be disabled on a per-application basis by adding
204<code>android:vmSafeMode="true"</code> in the <code>application</code>
205tag in <code>AndroidManifest.xml</code>.  This can be useful if you
206suspect that JIT compilation is causing your application to behave
207incorrectly.
208
209
210<h2><a name="dp">Deadlock Prediction</a></h2>
211
212<p>If the VM is built with <code>WITH_DEADLOCK_PREDICTION</code>, the deadlock
213predictor can be enabled with the <code>-Xdeadlockpredict</code> argument.
214(The output from <code>dalvikvm -help</code> will tell you if the VM was
215built appropriately -- look for <code>deadlock_prediction</code> on the
216<code>Configured with:</code> line.)
217This feature tells the VM to keep track of the order in which object
218monitor locks are acquired.  If the program attempts to acquire a set
219of locks in a different order from what was seen earlier, the VM logs
220a warning and optionally throws an exception.
221
222<p>The command-line argument is set based on the
223<code>dalvik.vm.deadlock-predict</code> property.  Valid values are
224<code>off</code> to disable it (default), <code>warn</code> to log the
225problem but continue executing, <code>err</code> to cause a
226<code>dalvik.system.PotentialDeadlockError</code> to be thrown from the
227<code>monitor-enter</code> instruction, and <code>abort</code> to have
228the entire VM abort.
229
230<p>You will usually want to use:
231<pre>adb shell setprop dalvik.vm.deadlock-predict err</pre>
232unless you are keeping an eye on the logs as they scroll by.
233
234<p>Please note that this feature is deadlock prediction, not deadlock
235detection -- in the current implementation, the computations are performed
236after the lock is acquired (this simplifies the code, reducing the
237overhead added to every mutex operation).  You can spot a deadlock in a
238hung process by sending a <code>kill -3</code> and examining the stack
239trace written to the log.
240
241<p>This only takes monitors into account.  Native mutexes and other resources
242can also be the cause of deadlocks, but will not be detected by this.
243
244
245<h2><a name="stackdump">Stack Dumps</a></h2>
246
247<p>Like other desktop VMs, when the Dalvik VM receives a SIGQUIT
248(Ctrl-\ or <code>kill -3</code>), it dumps stack traces for all threads.
249By default this goes to the Android log, but it can also be written to a file.
250
251<p>The <code>dalvik.vm.stack-trace-file</code> property allows you to
252specify the name of the file where the thread stack traces will be written.
253The file will be created (world writable) if it doesn't exist, and the
254new information will be appended to the end of the file.  The filename
255is passed into the VM via the <code>-Xstacktracefile</code> argument.
256
257<p>For example:
258<pre>adb shell setprop dalvik.vm.stack-trace-file /tmp/stack-traces.txt</pre>
259
260<p>If the property is not defined, the VM will write the stack traces to
261the Android log when the signal arrives.
262
263
264<h2><a name="dexcheck">DEX File Checksums</a></h2>
265
266<p>For performance reasons, the checksum on "optimized" DEX files is
267ignored.  This is usually safe, because the files are generated on the
268device, and have access permissions that prevent modification.
269
270<p>If the storage on a device becomes unreliable, however, data corruption
271can occur.  This usually manifests itself as a repeatable virtual machine
272crash.  To speed diagnosis of such failures, the VM provides the
273<code>-Xcheckdexsum</code> argument.  When set, the checksums on all DEX
274files are verified before the contents are used.
275
276<p>The application framework will provide this argument during VM
277creation if the <code>dalvik.vm.check-dex-sum</code> property is enabled.
278
279<p>To enable extended DEX checksum verification:
280<pre>adb shell setprop dalvik.vm.check-dex-sum true</pre>
281
282<p>Incorrect checksums will prevent the DEX data from being used, and will
283cause errors to be written to the log file.  If a device has a history of
284problems it may be useful to add the property to
285<code>/data/local.prop</code>.
286
287<p>Note also that the
288<code>dexdump</code> tool always verifies DEX checksums, and can be used
289to check for corruption in a large set of files.
290
291
292<h2><a name="general">General Flags</a></h2>
293
294<p>In the "Honeycomb" release, a general mechanism for passing flags to
295the VM was introduced:
296
297<pre>adb shell setprop dalvik.vm.extra-opts "flag1 flag2 ... flagN"</pre>
298
299<p>The flags are separated by spaces.  You can specify as many as you want
300so long as they all fit within the system property value length limit
301(currently 92 characters).
302
303<p>The extra-opts flags will be added at the end of the command line,
304which means they will override earlier settings.  This can be used, for
305example, to experiment with different values for <code>-Xmx</code> even
306though the Android framework is setting it explicitly.
307
308<address>Copyright &copy; 2008 The Android Open Source Project</address>
309
310</body></html>
311