Android game development part 2 (DalvikVM performance)

In this part we will talk about DalvikVM performance.

DalvikVM is a Java virtual machine for Android. The main disadvantage of DalvikVM is Garbage collector. Garbage collector when it is called freeze application on x00 milliseconds. This freezing is not acceptable for real time games so the we should minimize memory allocation. “Memory allocation tracker” tool help us with this work.

When I had started improvement of one android game I was surprised that almost everything allocates memory: Strings, Collections, enhanced for, even casting from int to Integer. As you can see we should avoid this things in performance-critical parts of code, in game this means that we can’t use this in game loop. To minimize memory allocation we should do:
  • replace all Strings by char[] because String is immutable class and when we pass it to the function or return it from function it creates a copy of this value, that provides memory allocation.
  • replace all Collections by fixed size array because iterators allocates memory for itself, and operations like add and remove also produce memory allocation
  • replace all for(Object e : list) by standard for(int i = 0 ; …) to avoid allocation for iterator
  • replace all Integer, Float, Double, Long, Boolean by int, float, double, boolean
The next improvement way is to see what method runs so long. Method profiling” tool help us with this work. And here you can see that work with enums is terrible slow, but we can replace it by static final int variable that will work 10 times faster.

Here is more another specific ways to improve performance, but I haven’t enough time to try it. So this ways to improve your game performance is only beginning.

The main articles about performance and game development for android you can find on android site and from google I/O video