30

Is Dalvik's memory model the same as Java's? I am particularly interested in whether reads and writes of reference and non-long/non-double primitive variables are atomic, but I would also like to know whether there are any differences between the two platforms' memory models.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193

3 Answers3

63

As of 4.0 (Ice Cream Sandwich), Dalvik's behavior should match up with JSR-133 (the Java Memory Model).

As of 3.0 (Honeycomb), most of the pieces were in place, but some minor things had been overlooked that would be difficult to encounter in practice (e.g. some edge cases in finalization).

As of 2.3 (Gingerbread), Dalvik was generally correct on uniprocessors, but some key features required for proper behavior on SMP hardware (e.g. proper final field handling) was missing.

Pre-Gingerbread, there were no memory barriers at all, and basic stuff like volatile long was broken.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • 9
    Vaguely related: Android+SMP Primer at http://developer.android.com/training/articles/smp.html – fadden Dec 03 '12 at 22:16
  • 1
    The SMP primer above is a must read for anyone dealing with memory shared between threads. Thanks so much for linking this! – asm Jan 21 '14 at 21:34
  • 3
    @fadden what about double check locking with volatile fix? like described here http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java will it work on Android? Any version? Will it work on ART? – Daniele Segato Sep 17 '14 at 17:06
13

There is a document in the Dalvik source which says:

From the point of view of a piece of code written in the Java programming language or targeted in the same way to .class files, the Dalvik VM aims to behave in a way that is fully consistent with the language's definition. That is, the code running in Dalvik will behave the same as it would have running in any other virtual machine.

Which should mean that the behaviour is the same as in proper Java. Whether it actually is or not, i have no idea.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
0

The specification says that all operations on 32 bit numbers (the non-double, non-long numbers) are atomic. There is no guarantee that operations on 64 bit numbers are atomic as well.

Jonathan B
  • 1,040
  • 6
  • 11
  • 4
    Which specification is this? If you mean the Java specification, i think the point of the question is to ask whether Dalvik implements that part of Java's semantics. If you mean some other specification, nny chance of a link? – Tom Anderson Jan 03 '11 at 21:01