Android garbage collection performance


Performance strategies for Android

1 Performance strategies for Android
1.1 Avoid object masterpieces
1.2 Avoid object destruction
1.3 Watch the garbage collection
1.4 Avoid getters and setters
1.5 Avoid method calls
1.6 Avoid collections
1.7 Silence the touch occasions
1.8 Avoid connects
1.9 Dereference if at all possible
1.10 Avoid float or double
1.11 Avoid single applies to native memory
1.12 Don’t add too much on optimizing things
1.13 Don’t alter thread focal points

Performance strategies for Android

Android garbage collection performance1 Android garbage collection performance

They are general tips and true for those Android programs, not only for jPCT-AE. It is a collection of products, which i discovered to become most significant throughout the introduction of AE. Keep in mind that this really is specific to real-time applications like games. If you’re not carrying this out, your mileage can vary.

If you’re accustomed to the very best pratices from the desktop Java world and praise OO and also the mightly VM a minimum of two times each day, the following advice can be quite disturbing for you personally. You’ve been cautioned…

An order is roughly from present with exotic…

Avoid object masterpieces

If at all possible, make your objects before beginning the particular game. Or at best attempt to reuse old objects. Object creation is costly on Android. Attempt to pool objects if at all possible. jPCT-AE offers some assistance with this. For instance, there is a static create()-method in SimpleVector instead of the constructor. That way enables jPCT to optimize SimpleVector usage to some small degree. Additionally, many techniques that return a Matrix or perhaps a SimpleVector appear in two flavours now: One which simply returns a brand new instance and something that can take a case being an argument and fills and returns that certain. It can save you the development of needless, short resided objects with this.
Avoid object destruction

If the object is not recommended any more, it’s liberated to be garbage collected. This really is really a positive thing because it frees some memory. However, on Android garbage collection is extremely slow. The different options are 100-200ms on a single run and free 10bytes in the finish. Therefore if possible, reuse objects and employ object pools to prevent a lot of objects being garbage collected inside your game’s primary loop. Both you and your gamers will watch a 100ms lag! jPCT-AE has already been enhanced to produceOrruin as couple of objects as you possibly can which are more parts.

Watch the garbage collection

As stated, it’s dog slow. It may be smart to place some finalize()/gc() calls after doing all of your setup work and before beginning the particular game. jPCT-AE offers some assistance with this within the util-package by means of the MemoryHelper.

Avoid getters and setters

If at all possible, help make your characteristics public or package public. Avoid using getters or setters, i.e. rather than




Avoid method calls

For straightforward techniques that you simply call very frequently inside a loop, envisage to inline the code. The VM does not do that for you personally and method calls are very costly.
Avoid collections

If at all possible, use arrays rather. More suitable individuals of native types like int[], lengthy[], float[]…not object arrays. Be careful for auto-boxing/-unboxing.

Silence the touch occasions

Touch occasions can ton the machine creating occasions as though there is no tomorrow. Envisage to give a Thread.sleep() inside your onTouchEvent()-method. Alter something of 10-20. It ought to assistance to greatly decrease touch occasions (evil objects being produced and disposed). Be cautious you don’t sleep too lengthy or perhaps your movement can get uneven.

However, touch controls suck anyway on older versions of Android, due to this bug!
Avoid connects

Write classes, not connects. It is really an Android application, probably a game title…you’ll begin with scratch anyway in your next project. You do not need this interface that enables for changing implementations as needed. You will not do that anyway, it simply costs you performance.

Dereference if at all possible

Avoid this:

for (int i= i b=gark.blubb[i]+12

Do that:

int[] garkBlubb=gark.blubb
int finish=garkBlubb.length
for (int i= i b=t+12

Avoid float or double

Just avoid them. If you cannot, use Android’s FloatMath rather than Math. If you need to divide multiple occasions through the same return, envisage to multiply with 1f/value rather.

Warning: Strange optimisation ahead…if you need to compare many floats multiple occasions, envisage to compare their int representation rather, i.e. use Float.floatToIntBits(). Based on the application as well as your hardware, this may by faster with a factor of two-3.

Avoid single applies to native memory

Attempt to backup your native data by a wide range within the VM’s memory. If that is too pricey (memory smart), use a minimum of a little buffer to buffer puts. It’s often cheaper to fill that buffer first and set that in a single call rather than doing single puts.

Don’t add too much on optimizing things

Be careful when optimizing…maybe your optimisation is just that fast, since it does various things compared to orginal version. Make sure that the enhanced code continues to be correct. Only optimize the critical sections. It is senseless to dereference all things in a setup method.

Don’t alter thread focal points

…just don’t. It’s really no good.