By Sumesh Kumar


2019-03-11 11:41:24 8 Comments

I read that apps sometimes access the native libraries for optimization purposes via the Java native interface.

As far as I understand, JNI just provides java wrappers around c/c++ libraries/binaries which are written closer to hardware hence faster and more efficient.

But how does it provide a performance boost, aren't JNI APIs called through Dalvik VM, so they do not run natively, they are still running inside a VM so there shouldn't be any difference.

1 comments

@Dan Hulme 2019-03-11 12:41:49

I think I see the source of your confusion. Dalvik VM isn't a virtual machine the same way as VMware or Virtualbox. In that kind of VM, VMware (or whatever) is pretending to be the whole computer. Every operation has to go through the VM, it has to be prevented from accessing the real hardware, and if it does access the real hardware, VMware has to do some work to pretend to be the real hardware, give the result to the OS, and have the OS give the result to the application. There's a whole copy of the OS running inside the VM, so it's an application, on an OS, on VMware, which is itself an application running on an OS, which is then on the real hardware.

Java and (by extension) Dalvik VM isn't like that. It's more like an abstraction of the real machine. The real hardware isn't being hidden from the application, and there isn't an extra copy of the OS there.

A Java application is just a process on the phone like a non-Java application. When a Java application makes a function call via JNI, Dalvik has to do a small amount of book-keeping to pass the objects used in the function call to the native code, and then the native code just runs in the process, with no VM involved at all. Then there's a little more book-keeping to get the result back to the Java code.

The code within the JNI call is as fast as any native code in a process where there's no Java at all. The only overhead is the book-keeping on entry to and exit from the call.

Related Questions

Sponsored Content

1 Answered Questions

Use of Dalvik VM in android

  • 2015-02-02 01:29:09
  • user1004985
  • 111 View
  • 0 Score
  • 1 Answer
  • Tags:   dalvik

4 Answered Questions

[SOLVED] Can you run 'normal' Java programs on Android?

  • 2014-02-17 18:11:31
  • scheurneus
  • 69736 View
  • 11 Score
  • 4 Answer
  • Tags:   java

2 Answered Questions

[SOLVED] How is the Android System running Java jars?

2 Answered Questions

[SOLVED] Removing dalvik from Android

  • 2015-08-27 12:13:17
  • Maciej Bielski
  • 1089 View
  • 0 Score
  • 2 Answer
  • Tags:   system dalvik

1 Answered Questions

2 Answered Questions

[SOLVED] Why does Dalvik virtual machine need to run in every process?

  • 2011-04-01 00:58:46
  • user4009
  • 2398 View
  • 2 Score
  • 2 Answer
  • Tags:   dalvik processes

Sponsored Content