• Advertisement
Sign in to follow this  

Question about use of JNI

This topic is 1950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I was doing some research about Open GL for Android and came across this users question on
StackOveflow : http://stackoverflow...ndroid-graphics

In his explanation, he states : "The math engine is in C (JNI)"

I was just wondering why would this user want to use a math engine written in C through JNI and not just
implement it in Java. Also, can someone help me better understand when you would want to use JNI and how it really works?

Thanks. Edited by Millionaire

Share this post


Link to post
Share on other sites
Advertisement

I was just wondering why would this user want to use a math engine written in C
Probably because doing low level math is faster in C than in Java.

Share this post


Link to post
Share on other sites

[quote name='Millionaire' timestamp='1355654760' post='5011221']
I was just wondering why would this user want to use a math engine written in C
Probably because doing low level math is faster in C than in Java.
[/quote]

Java game developers on the desktop tend to use Java directly for their math stuff. But the Dalvik VM on Android is not as performant as the Oracle VM or OpenJDK on the desktop. So time critical routines are often split off into C.

Share this post


Link to post
Share on other sites
Runtime efficiency aside, it (generally) takes a lot less development time to use existing code than it does to write new code. If an entire system has already been implemented in C it would almost certainly be faster to use that than it would to re-write the entire thing in Java.

Share this post


Link to post
Share on other sites
Just so you guys know, but Android has a per-process limit of 16-24 megabytes of RAM usage. Try not to get over that limit. If you are going to call JNI functions often and allocate/free memory on each call, you'd need to keep JNI initialized as malloc-ing and free-ing is expensive, I found that out the hard way.

And if you need to update Java from the JNI side, you'd need to free the local reference.

Share this post


Link to post
Share on other sites

[quote name='Millionaire' timestamp='1355654760' post='5011221']
I was just wondering why would this user want to use a math engine written in C
Probably because doing low level math is faster in C than in Java.
[/quote]

as a rule of thumb that isn't the case, there are only 2 really important exceptions, SIMD and trigonometry(in some ranges) on x86 (trig on other platforms perform equally).

it is very important to always make a high level interface for the JNI code since the overhead of JNI is fairly high. (if you do single operations through JNI you will almost always lose performance) Edited by SimonForsman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement