Android Question Way to handle with CPU intensive tasks ?

JordiCP

Expert
Licensed User
Longtime User
Hi all,

I am developing an app that uses CPU intensive tasks. In a high-end device, it behaves ok.

But in other mid-end devices, it is quite slow.

I have done my own java libraries and passed some critical finctions to the NDK, now it is quite faster but not enougth

Is there any way to pass "intense calculations" to another thread or service so that it does not affect my app UI?

Any suggest will be welcome
 

thedesolatesoul

Expert
Licensed User
Longtime User
Is there any way to pass "intense calculations" to another thread or service so that it does not affect my app UI?
You will need to use the Threading library for this.
If the intense calculation is in your java library, you can spawn a runnable in the library so it offloads everything to another thread.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Thank you for the fast answer!

I just could manage to do the lib and also working with the NDK, but I am not an expert, so I just know how to do calls, but no idea about how to spawn in Java and other issues...


The exact situation is:

The app uses the camera. I have a continuous camera_preview event which I want to treat in order to do some real-time effects.
When I get the preview event, and its associated data:

Inside the Camera_preview event, (just to not block this event) I do a callSubDelayed to a SubRoutine which​

  • Converts from NV21 to ARGB (--> using my library --> which makes a call to another subroutine using the NDK)
  • Do some stuff with this data (--> using my library)
  • when it is done, already back to B4A program, draw it on another layer which is over the preview image
The problems:
  1. I get aprox 10fps on a HTC one (acceptable), but only about 5fps on a Galaxy II, and less than three on my old chinese Tablet
  2. In the two later ones, the UI is also much less responsive

So, I guess I have to work in two directions

  • Keep optimizing the processing of the image, but I think there will be a limit on this
  • If I understood well, by throwing this processing from a diffrent thread, I will leave the UI from the main one more responsive, and this will improve the result for problem (2). Is it right?


There are two different types of calculations performed at different moments (but very often), which take, let's say, 40msec and 20msec. But as I throw them very often, i wonder if Does the threading approach give a significant overhead?


About efficiency....do you know of tips (or where can I find) about making "java subroutine calling" more efficient? I think sometime I read that parameter passing (and I do pass a lo of parameters) has a significant overhead (but I am not sure at all about it, just have it in my mind). It would not be a concern in functions which take about 1 second, but very significant on other scenarios in which the function is called repeatedly. Perhaps if the java library had a reference to those objects it would be easier? Just a thought...


Thanks a lot. I don't use to write in the forum but I do highly appreciate the help that you and other experts give to the others!
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Great work!

The HTC One is 1.7 GHz Quadcore, GS2 was DualCore 1.2 GHz, and not sure what your chinese tablets are but chances are they are single core.
Threading will give you an advantage mainly on multicore, but sometimes on single core. But if a single core is busy processing your pixels anyway, chances are it wont help you smooth out the UI. In fact I think using more threads will help the HTC One the most.

I think what you should do is (after optimizing the pixel crunching), is to reduce the resolution on the older hardware. Chances are the camera will be less powerful and worse focus anyway.

I dont think threading will give you overhead, but it wont help in cores that dont implement some kind of threading or virtual threading.

About subroutine calling I am not a good person to ask. I thought alot of times only the reference is passed. I know that Reflection adds some overhead which can be accounted.

I hope some one else can also add more helpful information to this discussion.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Thanks, but a lot to do yet :)

I have been able to start a thread and do some things in it, so I think it will improve the overall behaviour, but....


When, in the thread, I make a call to a method in my library passing it some variables, the program crashes and returnss

Fatal signal 11 (SIGSEGV) at 0x6b951000 (code=2), thread 7561 (B4A Thread 1)

(I see it in Release mode, cause debugging does not work with thread library, but anyway connected to B4A through the usb)

Any idea?


Will keep trying
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
At the end it worked!

Believe or not, the crash came when I called a library subroutine that accessed the JNI (Calling it from outside the thread it works and from inside not, but possibly I am doing something nasty there and it detects so when called from the thread). I changed it and now the UI is much more responsive, even in my tablet!!

The drawback is that now the process takes longer to execute, since there are parts of it which handle bitmaps and must call them with RunOnGUIThread, but that is another question

Thanks a lot for the suggestion !!
 
Upvote 0
Top