Search This Blog

Friday, December 6, 2013

Speed Up "Android JNI" Tricks and Tips

Recently, I got a job to speed up application that using JNI. Many people thought by using native code, it should run faster than Java but I found out that is not always true.

First, the passing parameters from Java level to native code are using time to convert and if you use JNI so often then that gonna be heavy load.

Second, you need to allocate/deallocate resources by yourself in native level(there is no  garbage collector in C/C++ ) and that takes time to do anyway.

Ok. Here are tricks and tips for speed up your Android JNI app. Hope it works for you too  ...

1. allocate/deallocate just one
   The bigger memory you need, the slower you are. So, if it's possible. Try to do allocate and release the resources just one time.


2.don't allocate too big block memory
    Check you code that you really need that much resource? Just use that it needs to be run properly.
for example, data = (unsigned char *)malloc( BLOCKSIZE + 100 );
May be +100 is not necessary.

3. don't use double
   I don't know why but in native code, double makes slow. I changed it type to normal "int" and it running a lot faster.(make sure it doesn't make wrong result)

4. set MAX priority to the thread
   I found that when garbage collector thread is running, some delay may occur(not much but it does go slower) So, try to set your JNI caller thread's priority to MAX that will (somehow) prevent garbage collector thread to run during JNI call.

5. try "ARM" mode
   In your Android.Mk file, add LOCAL_ARM_MODE := arm line and rebuild the code. I found out that the default Thumb mode is slower than Arm mode. However, I'm not sure that it gonna be true for all device.


That is for today !! and may the Force be with you all :D

No comments:

Post a Comment