Jump to content

  • Log In with Google      Sign In   
  • Create Account


Question about use of JNI


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Millionaire   Members   -  Reputation: 117

Like
0Likes
Like

Posted 16 December 2012 - 04:46 AM

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, 16 December 2012 - 04:53 AM.


Sponsor:

#2 J. Evola's Apprentice   Members   -  Reputation: 307

Like
2Likes
Like

Posted 16 December 2012 - 05:11 AM

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.

#3 Aldacron   GDNet+   -  Reputation: 3065

Like
3Likes
Like

Posted 16 December 2012 - 05:46 AM


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.


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.

#4 jbadams   Senior Staff   -  Reputation: 17285

Like
2Likes
Like

Posted 16 December 2012 - 06:33 AM

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.

#5 farmdve   Members   -  Reputation: 194

Like
1Likes
Like

Posted 16 December 2012 - 11:13 AM

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.

#6 SimonForsman   Crossbones+   -  Reputation: 5804

Like
1Likes
Like

Posted 16 December 2012 - 01:25 PM


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.


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, 16 December 2012 - 01:35 PM.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#7 Millionaire   Members   -  Reputation: 117

Like
0Likes
Like

Posted 16 December 2012 - 09:25 PM

Thanks everyone, this really clears up some questions. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS