Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can anyone shed some light on HotSpot/JNI?

843811Aug 31 2003 — edited Aug 31 2003
Hello,

I have just wrapped a C API in JNI.
It pumps messages into a callback, which are then forward to the Java side via a env->CallIntMethod(object, method, jlong, jlong).
On the java side a single accessor object is created to access the fields in the C messages. The accessors are of course JNI. A single object is created instead of a new object per message. Code snippets follow below.

Trouble is, things are incredibly slow. A C test runs in 16 seconds, the Java version takes 6 minutes!
A test which returns from the callback right away takes C 15 seconds and Java 26 seconds - still too long for the one single JNI call.

I have timed some native calls from Java to C, and found JNI calls about 7-8x slower than calls to a java method.
Even with this knowledge, I can't account for the huge performance hit.

Is is possible that my java callback, which is called from C, and all my accessor objects which are created once and call C from Java, are not being compiled to machine code but are being interpreted? Does hotspot do its stuff on existing objects? I assumed so, but I am not sure now.

Some code snippets follow (I can't post actual code because of NDA stuff)

// Class is created once at the beginning of processing, and may see
// +50million messages up to 200million or so.
public class ABCD {
private long swigCPtr;
...
// I call this on each new message.
void setCPtr(long ptr) {
swigCPtr = ptr;
}

// Accessor - goes to JNI to grab the C data.
public int getUserData1() {
return xyzJNI.get_ABCD_UserData1(swigCPtr);
}
}

And C++ code calling Java:
int JavaListener::onMessage(const System system, const Message message) {
return env->CallIntMethod(jlistener, jonMessage, (jlong)system, (jlong)message);
}

So now possible ideas for solutions if I can't find out what is going on:
1. Direct memory buffers. Copy the most often used data into a buffer and then unpack it on the java side.
2. UDP. Forward the messages via a UDP/localhost connection which I believe uses direct memory to transfer - but this is a pain, most of my JNI wrapper is autogenerated with SWIG. Is networking using JNI in java or some internal magic?

Any comments are welcome, as I don't want to do anything for a bit - annoying having a working Java wrapper, but unusable in any application I have.

-Ed
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 28 2003
Added on Aug 31 2003
1 comment
190 views